Home > Back-end >  Setting both x and y values of a histogram plot
Setting both x and y values of a histogram plot

Time:11-22

I am trying to make a histogram of my data but am unable to make a plot with the correct x and y values and I get an error when I try and set both in aes() that only one can be delineated

My data table looks something like this:

df <- data.frame(mz = c(10, 10, 11, 11, 12, 12, 14, 14), name = c('a', 'b','a', 'b','a', 'b', 'a', 'b'), value = c(1000, 1200, 900, 1100, 1000, 1300, 1115, 950))

(ie each variable a or b has a value for each mz)

and I would like to create a histogram where the x axis is mz and the y axis is value where the two variables are a vs b ie based on the column "name". However when I try to make a histogram via:

ggplot(df)   geom_histogram(aes(x=mz, fill=name))

I get an output that has "count" as its y axis and is unrelated to the intensity, however I get an error message if I try and make the aes(x=mz, y=value) that I cannot have both an x and y aesthetic.

Is there a better way of doing this/is a pivot table not the best input?

CodePudding user response:

Try geom_col

ggplot(df)   geom_col(aes(mz, value, fill=name), position=position_dodge())

bar plot

  • Related