Reputation: 39
I need help with a graph in R with ggplot2 because after trying several things, I don't know how to do. I have the following dataframe:
df <- data.frame(
TITLE = c("GRADUATE IN TITLE 1", "GRADUATE IN TITLE 2", "GRADUATE IN TITLE 3",
"GRADUATE IN TITLE 4", "GRADUATE IN TITLE 5"),
X2011 = c(1, 2, 3, 4, 5),
X2012 = c(3, 4, 5, 1, 2),
X2013 = c(1, 2, 5, 3, 4),
X2014 = c(1, 3, 4, 2, 5),
X2015 = c(5, 1, 2, 4, 3)
)
What I want is to make a graph that has all the "TITLE" values in the Y axis, each of the years (2011, 2012, 2013 ...) in the X axis, and for each row corresponding to a "TITLE", paint a horizontal line so that the crossing point of each year is the position corresponding to the value of that year column for that "TITLE", so all the lines on the graph go up or down depending on the values corresponding to this column.
Upvotes: 0
Views: 109
Reputation: 206526
ggplot
likes it when your data is "tidy", that is each row should represent one point to be plotted. One easy way to convert your data to a more plotting-friendly format is to melt it using the reshape2
package.
library(ggplot2)
ggplot(reshape2::melt(df), aes(variable, TITLE, group=value)) + geom_line()
Then you can use the standard line geom and use groups=
to connect the values across the different categories.
This produces
Upvotes: 3