0
votes

I have a dataframe in R with three columns, label indicates the x-axis label, values would indicate the bar height for a barplot, and and a color value. For instance:

    label     value       color
1  Label1 0.9645477 0.434680035
2  Label2 1.0816859 0.070992644
3  Label3 1.0043559 0.923586653
4  Label4 1.0065987 0.884469520
5  Label5        NA          NA
6  Label6 0.9537397 0.307786218

I'd like to make a bar plot of the values where the bars are colored by the value in the color column. The color column always ranges from 0 to 1 and I'd like to make discrete colors, on a green to red spectrum, for bins 0-0.05, 0-0.1, etc. And then I'd like to have a legend associated with the barplot. For the cases of NA, I'd like there to just be an empty spot on the plot with no bar.

How would I do this?

1

1 Answers

1
votes

I would suggest you use the ggplot2 library:

library(ggplot2)
ggplot(dataframe) + geom_bar(aes(x=label, y = value, fill = color), stat = "identity")

the aes parameter defines the mapping within geom_bar, using fill = color will fill the bars based on the color column in your dataframe (setting the color parameter will change the outline of the bars, not the fill). The stat parameter outside of aes defines whether geom_bar computes a histogram or a barchart.