1

I'm quite new to R, and there has been a question similar to mine asked before, however it doesn't quite get to what I need.

I have a table as follows:

enter image description here

I wish to plot the Value, and Threshold alongside each other on the X-axis for each metric, so effectively, I will have three pairs of plots on the X-axis. I have attempted to use reshape2 and ggplot2 for this as follows:

library(reshape2)
df <- melt(msi, id.vars="Average Metric Value (Abbr)")
# I get an error message, but the output seems ok.

library(ggplot2)
ggplot(df, aes(x="Average Metric Value (Abbr)", y=value, fill=variable)) + geom_bar(stat='identity', position='dodge')

The output graph is as follows:

enter image description here

I'm sure I can work out how to separate each of the three pairs later, but as you can see, I don't have the metric names for each of the three pairs along the x-axis, and I am missing the first "Value" bar, presumably because it equals the same as the second and I am only getting unique values plotted.

How do I get around that and have the names of each metric beneath each pairs of values?

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
MJ_Macca
  • 27
  • 8

1 Answers1

0

We can do this by placing inside the aes_string or use backquotes in the aes for those columns that have spaces in its names

library(dplyr)
library(tidyr)

gather(msi, variable, value, Value:Threshold) %>%
        ggplot(., aes(x= `Average Metric Value (Abbr)`, 
                      y=value,
                      fill=variable)) +
            geom_bar(stat='identity', position='dodge')

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you for such a quick response. I get the following error however: > ggplot(df, aes_string(x="Average Metric Value (Abbr)", y="value", fill="variable")) + geom_bar(stat='identity', position='dodge') Error in parse(text = x) : :1:9: unexpected symbol 1: Average Metric ^ – MJ_Macca Mar 21 '18 at 06:11
  • Thank you so much! Sorry, I haven't used this much and missed most of your answer! Thank you kindly! – MJ_Macca Mar 21 '18 at 06:13