How can I represent in R two quantitative variables and color the plot according to a categorical variable?

595 views Asked by At

'''

my_data<-iris

''' '''

length.ratio<- my_data$Sepal.Length/my_data$Petal.Length
width_ratio<-my_data$Sepal.Width/my_data$Petal.Width

''' '''

my_data$Species<-as.factor(my_data$Species)

''' #Attempt of plotting with ggplot2

'''

ggplot(my_data,aes(length_ratio,fill=Species))+theme_bw()+
  facet_wrap(width.ratio~Species)+ geom_density(alpha=0.5)+
  labs(x="Width Ratio", y="Length Ratio")

'''

#Actually, I neither know which 'geom_plot' is the best option.

1

There are 1 answers

3
Duck On BEST ANSWER

It looks like you are looking for a scatter plot. So try this and always try to keep your variables in your dataframe. If you store your variables inside same dataframe you do not have to create a new factor. Here the code:

library(ggplot2)
#Data
my_data<-iris
#Compute variables
my_data$length.ratio<- my_data$Sepal.Length/my_data$Petal.Length
my_data$width_ratio<-my_data$Sepal.Width/my_data$Petal.Width

The plot:

#Plot
ggplot(my_data,aes(width_ratio,length.ratio,color=Species))+
  geom_point(alpha=0.5)+
  theme_bw()+
  facet_wrap(~Species,scales='free')+
  labs(x="Width Ratio", y="Length Ratio")

Output:

enter image description here

And if you want to study density try this:

#Plot 2
ggplot(my_data,aes(length.ratio,color=Species,fill=Species))+
  geom_density(alpha=0.5)+
  theme_bw()

Output:

enter image description here