0

I'm working with a dataframe with several columns, and am trying to use each column as a different aspect of the plot.

Specifically, I want to use one column as my x-axis, another column as my y-axis, and another column as a grouping factor.

data("iris")
str(iris)
iris$species[75:150] = sunflower

So in this dataset for example, I would want to plot Petal.Length vs Petal.Width as a barplot, and group by species. Any advice on this (and perhaps a tutorial page where I could learn how to become familiar with plotting with ggplot) would be super helpful. Thanks!

I asked a previous question and tried using pivot_longer() to reshape my dataframe; however, it turns the column names as the group which is not what I want.

Luis Loza
  • 11
  • 1
  • `library(ggplot2); ggplot(iris, aes(Petal.Length, Petal.Width, fill = Species)) + geom_col()` – Allan Cameron Feb 16 '23 at 14:14
  • what do you mean by "group by"? Do you want them stacked based on that column? Do you want them to have the same color? https://r-graph-gallery.com/48-grouped-barplot-with-ggplot2 – M-- Feb 16 '23 at 14:49
  • Ah - have them all plotted on the same plot, but colored differently based off the 'species' variable! Although I supposed a line plot or something else would be better than a bar plot... – Luis Loza Feb 16 '23 at 14:53

0 Answers0