0

I would like to plot some data with ggplot2. My data is originally in wide format, and so I would like to transform it to long format first.

Somehow "melt" produces a table I cannot use with ggplot2 as desired. I guess I am using it wrong, but I could sadly not find out, how to solve the problem.

I would like to plot the variables s1 and s2 vs the wavelength. This is a minimal half working example:

#Generate fake data:
data <- cbind(wavelength = seq(1,10), s1 = seq(21,30), s3 = seq(41,50))

#Convert to long format:
#install.packages("reshape2")
library(reshape2)
datLong <- melt(data          = data,
            id.vars       = c("wavelength"),
            measure.vars  = c("s1","s2"),
            variable.name = "variable",
            value.name    = "value")

#install.packages("ggplot2")
library("ggplot2")

#The following delivers the error "Error: Attempted to create layer with no stat."
 ggplot(data = datLong, mapping = aes(x = wavelength, y = value)) + layer(geom = "point") 

#This plots something, but it seems that "melt" produced a wrong format as wavelength, s1, s2 are on the x-axis.
ggplot() + layer(data = datLong, mapping = aes(x=Var2, y=value), geom = "point", stat = "identity", position_dodge(width = 3))

It would be very great if you could explain the problem.

Thank you very much!

2
  • 2
    The issue is that data is a matrix. So you are actually using melt.array and not melt.data.frame. The argument names differ and hence the result is not what you are looking for, see ?reshape2::melt for details. Commented Jan 25, 2019 at 8:50
  • I'm not sure but it's rare to find a layer created as in case 1. Normally you just call the geom as done in my answer. Two fails because you set the x-axis to Var2(which isn't wavelength I think). Commented Jan 25, 2019 at 8:52

1 Answer 1

1

Is this what you had in mind?

library(ggplot2)
library(dplyr)
library(tidyr)
#or just library(tidyverse)
as.tibble(data) %>% 
  gather("Var","Val",-wavelength) %>% 
  ggplot(mapping = aes(x = wavelength, y = Val,col=Var)) + geom_point()

Using reshape2 and the tidyverse

as.tibble(data) %>% 
  melt(id.vars=c("wavelength")) %>% 
  ggplot(mapping = aes(x = wavelength, y = value,col=variable)) + geom_point()
Sign up to request clarification or add additional context in comments.

6 Comments

exactly what I was going to do, except maybe use as_tibble rather than as.data.frame to keep it in the tidyverse family.
Edited. Thanks! Used as.tibble though.
I'm just being picky now, but as.tibble is depreciated.
Thank you very much! Works perfect! Still I am interested in what was the mistake I made.
@Roland it was in a non data.frame format. I've made another comment below the question. You can see this [,1] in the original code you provided. This suggests the data is not a dataframe. You can use str to see the object's data type. Use class(data) to see its class
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.