Chouette
Chouette

Reputation: 153

How can I draw both line and point by ggplot according to one variable (R)?

I want to draw the "measured"with point, and the "simulated"with line, but I don't know how to select or subset under ggplot. Could anyone help me please? Thank you!

x-Date,y-SWCCurrent codeCurrent graph

library(foreign)
library(ggplot2)
library(dplyr)
library(readxl)
library(scales)
X0_40cm <- read_excel("C:/Rstudy/For_0-40cm.xlsx")
View(X0_40cm)
gg <- ggplot(X0_40cm,aes(Date,SWC))+
  geom_point(aes(col=Condition))+
  scale_y_continuous(limits = c(0,0.4),labels=percent) +
  labs(title="Soil water content (0-40cm)",y="Soil water content",x="Date")+
  theme_update(plot.title=element_text(hjust=0.5))
gg

Upvotes: 0

Views: 96

Answers (1)

Ben
Ben

Reputation: 30474

It would be helpful to include your X0_40cm data using dput() instead of an image.

One quicker way to subset for purposes of plotting is to specifically include data=subset... for both geom_point and geom_line.

Also note to include group=1 in your aesthetic since you want a line plotted.

ggplot(X0_40cm, aes(Date,SWC,group=1))+
  geom_point(data=subset(X0_40cm, Condition=="Measured"))+
  geom_line(data=subset(X0_40cm, Condition=="Simulated"))+
  scale_y_continuous(limits = c(0,0.4),labels=percent) +
  labs(title="Soil water content (0-40cm)",y="Soil water content",x="Date")+
  theme_update(plot.title=element_text(hjust=0.5))

Upvotes: 1

Related Questions