I'm somewhat surprised that I can't find a solution to this problem on SO, but I've tried every search term that I think might apply. However, I may not be using the correct search terms so forgive me if this is a duplicate, and please point me in the correct direction. I have data that is grouped by sample and each sample has one value for each category, of which there are many. Here is an example dataframe (note that the number of samples and number of categories are usually different):
df <- data.frame( sample = c( "one", "two", "three", "four" ),
cat_1 = c( 2, 4, -6, 2 ), cat_2 = c( 1, 2, 2, 1 ),
cat_3 = c( 5, -5, 7, 2 ) )
I'm trying to create a plot where the x-axis has discrete points for each category, the y-axis is the value for all samples at each category, and those values for each sample across the categories are connected by lines of a color I can define.
It seems like ggplot2
is the way to go here, but I can't find a way to get this to work out the way I want. It seems like I want colnames( dd )
to be the x-axis variable when using aes()
but that warns me that x
and y
are not the same length. Seems like this should be quite simple to do, but I can't figure it out.
EDIT: I've come across this post Plotting multiple variables from same data frame in ggplot where the answer shows the exact type of plot I want to make, but I can't figure out how to use melt
to change the my data frame into a format that puts the column names, cat_1
, cat_2
, cat_3
, as the id.vars
.