简体   繁体   中英

plot multiple variables by group [r]

i want to plot multiple plots, where in each plot i have observations of a set variable for different time sets in function of a distance

short example of my df:

year <- c("2018","2018","2018","2018","2019","2019","2019","2019")
polutatnt <- c("NO2","NO2","SO2","SO2","NO2","NO2","SO2","SO2")
radius <- c("500m", "1000m","500m", "1000m","500m", "1000m","500m", "1000m")
value <- c(0.5,0.8,0.1,-0.2,0.3,-0.6,0.2,-0.2)
    
df <- data.frame(year,polutatnt,radius,value)

i would like to have one plot for each polutant, where i would have one line for each year in function of distance. i tried this line of code but i get a waring and empty plots:

ggplot(df, aes(radius, value, col = year)) + 
  geom_line()  + facet_grid(polutatnt ~.)

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

According to the requirements described by you, this is what you want:

[EDIT] All of the blue points and red points linked

ggplot(df, aes(radius, value, color = year, group=polutatnt, shape=year)) +
  geom_point(size=3) + geom_line(aes(group = year)) + facet_grid(polutatnt ~.)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM