Reputation: 35
My question could be elementary yet it has been giving a lot of headache for the last many hours, thus any help would be very much appreciated.
I have a data frame with ~150 columns and ~1000 rows. Each [i,j] cell of the data frame contains a numerical value, apart from the values in the 1st column of the data frame which contain a unique ID (unique for each row of the data frame)
I would like to draw a chart where each row of the data frame would be plotted on a separate chart (plot) on its own (i.e. for each row a plot will be created having its own axis, with the values of the row plotted on the y-axis and the column names of the data frame as labels on the x-axis). Then each of these individual charts will be positioned below the previous one.
For example, if we assume the data frame is the following:
Jan = c(2, 3, 5)
Feb = c(8, 9, 12)
Mar = c(2.2, 7, 3)
Apr= c(1, 3.7, 23)
df = data.frame(Jan, Feb, Mar, Apr)
then my goal would be to have 3 plots one below the other, each containing a line passing through 4 points corresponding to the values of each of the 4 months (Jan, Feb, Mar, Apr). For example the 1st plot would have a line passing from the values [2, Jan], [8, Feb], [2.2, Mar] and [1.0, Apr]. Similarly for the next two plots.
Could someone point me to the correct direction with some hints on how to meet my goals?
Thank you very much!
Upvotes: 0
Views: 5051
Reputation: 3938
Here is a solution using melt
from the reshape2
package on your example.
It might be harder to have 1000+ facets (rows) and 150+ x-ticks (columns).
df = cbind(df, row_number = seq(1, 3))
df_melt = melt(df, id = "row_number")
ggplot(df_melt, aes(x = variable, y = value, group = row_number)) +
geom_line(stat = "identity") + facet_wrap(~row_number, ncol = 1, scales = "free")
Upvotes: 2