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.
CodePudding user response:
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
ggplot(data.frame(panel=13:22,
total=c(10,20,30,40,50,60,70,80,90,100)),
aes(panel,total)) geom_point()
CodePudding user response:
If you want to keep the PANEL variable name you can use this code:
ggplot(data=tograph,aes(x=tograph$PANEL,y=tograph$total))
geom_line()