Home > OS >  How to ggplot two different objects that are within the same column?
How to ggplot two different objects that are within the same column?

Time:10-27

#Data frame is Flower. Trying to scatter plot the growth (height, rate) of two different species of flowers (Rose, Daisy).

FlowerPlot <- ggplot(Flower, aes(Height, Rate)) 
  geom_point(size=7, color = red) 
  theme_bw() 
  theme(text = element_text(size=15)

#This gives me a scatterplot with all the species (Roses, Daisy) as one. How do I plot two species as two different colors on the same plot?

I tried Factor and it failed.

Flowerplot(flowers, aes(Height, Rate, color=factor(Flower$Species))) 
  geom_point(size=7, color = factor(Flower$Species) 
  theme_bw() 
  theme(text = element_text(size=15))

#Is there a way to do something like Flower$Species$Rose? (I know I can't actually do this but something to this extent? Note: I cannot post the entire data frame because it's massive.

CodePudding user response:

Your color argument should probably be the Species column instead of red.

FlowerPlot <- ggplot(Flower, aes(Height, Rate)) 
  geom_point(size=7, color = Species) 
  theme_bw() 
  theme(text = element_text(size=15)

Don't think of the color argument as 'what color should I use to plot?'. Instead, think of the argument as asking `what feature of the data should I use to group similar points together?'

By using the Species column, you are telling ggplot: "All points with the same species value should have the same color."

When you say that factor(Flower$Species) did not work, can you explain / show what it did and what you were expecting?

CodePudding user response:

Ans: Need to remove everything from geom_point other than size and add one more ) after Species

Flowerplot <- ggplot(Flower, aes(Height,
                                 Rate, color=factor(Species))) 
  geom_point(size=1) 
  theme_bw()
  • Related