Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

r - Plotting geom_line() and geom_point() - data of different lengths

I am trying to plot a timeseries in ggplot such that the yearly values are connected with geom_line() and the totals appear as separate geom_point() at the far right of the x-axis.

I have tried to subset the data within the aesthetic, but get the error:

Aesthetics must be either length 1 or the same as the data (1): x, y

I have also tried to use two different data frames but get a similar error. Sorry if this is a basic question, but I have had no luck finding a solution.

Please see the dummy dataset and ggplot2 script below. I would like the final plot to look like this but without the line connecting '2017' and 'total', and preferably without having to resort to editing the output in Adobe Illustrator!

Any help appreciated.

library(ggplot2)

##synthetic data
Year <- seq(1996,2017)
var1 <- sample(0:10,length(Year), replace=TRUE)
var2 <- sample(0:10,length(Year), replace=TRUE)
var3 <- sample(0:10,length(Year), replace=TRUE)
var4 <- sample(0:10,length(Year), replace=TRUE)
total <- c("total",sample(0:10,4, replace=TRUE))


dat <- data.frame(Year, var1,var2,var3,var4)
dat <- rbind(dat,total)


plt <- ggplot(data=dat, aes(x=Year))
plt <- plt +
    geom_point(aes(y=var1, colour = "var1")) +
    geom_point(aes(y=var2, colour = "var2")) +
    geom_point(aes(y=var3, colour= "var3")) +
    geom_point(aes(y=var4, colour = "var4")) +
    geom_line(aes(y=var1, group=1, colour = "var1")) +
    geom_line(aes(y=var2, group=1, colour="var2")) +
    geom_line(aes(y=var3, group=1, colour="var3"))+
    geom_line(aes(y=var4, group=1, colour= "var4")) +
    scale_colour_manual("",
        breaks = c("var1", "var2", "var3", "var4"),
        values = c("#d7191c","#fdae61","#abd9e9","#2c7bb6")) 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Best to keep the total data in another data.frame. Reshaping the data greatly simplifies the ggplot command.

##reshape data
dat <- data.frame(Year, var1,var2,var3,var4)
dat <- tidyr::gather(dat, key = var, value = value, -Year)

##data.frames of totals
total <- data.frame(Year = max(Year) + 1, var = paste0("var", 1:4), value = sample(0:10,4, replace=TRUE))

dat <- rbind(dat,total)


plt <- ggplot(data=dat, aes(x=Year, y = value, colour = var)) +
  geom_point() +
  geom_line() +
  geom_point(data = total) +
  scale_colour_manual("", values = c("#d7191c","#fdae61","#abd9e9","#2c7bb6")) +
  ##change xaxis to show "total"  
  scale_x_continuous(breaks = c(seq(min(dat$Year), max(dat$Year), 2), total$Year[1]), 
                     labels = c(seq(min(dat$Year), max(dat$Year), 2), "Total"))


plt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...