I'm trying to create a simple line plot in ggplot2 with a dataframe, but the results aren't appearing as expected.
Here is the code to reproduce the data:
tograph<-data.frame(PANEL=13:22,total=c(10,20,30,40,50,60,70,80,90,100))
And when I graph the results in ggplot2, it just creates a straight vertical line at PANEL=1 (SO won't let me post images at this time)
ggplot(data=tograph,aes(x=PANEL,y=total))+geom_line()
As a sanity check, I ran some example data I found on a different post to make sure it wasn't something unique to my installation of R and it worked fine
xValue <- 1:10
yValue <- cumsum(rnorm(10))
data <- data.frame(xValue,yValue)
# Plot
ggplot(data, aes(x=xValue, y=yValue)) +
geom_line()
Similarly, graphing my intended data in base R also works fine, but I'd prefer to set it up in ggplot2 for aesthetic reasons:
plot(tograph$PANEL,tograph$total,type="l")
Any help with fixing this is much appreciated.
It looks like PANEL
is a protected data.frame variable name in ggplot2. Use a different variable name, like panel
.
(I suspect it has something to do with how ggplot2 processes the incoming data to prepare to plot it. e.g. from here:
compute_layout
: Based on layer data compute a mapping between #' panels, axes, and potentially other parameters such as faceting variable #' level etc. This method must return a data.frame containing at least the #' columnsPANEL
,SCALE_X
, andSCALE_Y
each containing #' integer keys mapping a PANEL to which axes it should use. In addition the #' data.frame can contain whatever other information is necessary to assign #' observations to the correct panel as well as determining the position of #' the panel.
ggplot(data.frame(PANEL=13:22,
total=c(10,20,30,40,50,60,70,80,90,100)),
aes(PANEL,total)) + geom_point()
ggplot(data.frame(panel=13:22,
total=c(10,20,30,40,50,60,70,80,90,100)),
aes(panel,total)) + geom_point()