简体   繁体   中英

How to combine line and bar chart for specific variable using R

I have a quite straightforward question. I would line to have a bar chart for var1, var2, var3 and var4 and a line chart for var5. Line and bars should be in the same chart. How can I get it keeping the long format with ggplot?

set.seed(1984)
start_date <- as.Date('2015-01-01')  
end_date <- as.Date('2017-01-01')  

date_yq <- as.Date(sample( as.numeric(start_date): as.numeric(end_date), 50, 
                           replace = T), 
                   origin = '1970-01-01')
var1 <- runif(50)
var2 <- runif(50)
var3 <- runif(50)
var4 <- runif(50)
var5 <- var1+var2+var3+var4

country <- c("AT","AT","AT","BE","BE","CY","CY","CY","DE","DE")
country_r <- rep(country, times = 5)
df <- data.frame(date_yq, country_r, var1, var2,var3,var4,var5)
View(df)

df_m <- gather(df, key="variable", value="value", c("var1","var2","var3","var4","var5"))


ggplot(df_m, aes(x=date_yq, y=value, colour=variable)) +
  geom_line()+
  geom_bar(stat = "identity")+
  xlab("Date") + 
  ylab("Value")

Edit: after some adjustments this is the code to reproduce the data:

 ggplot(data = df_m, aes(x = date_yq, y = value,fill=as.factor(variable))) +
  geom_bar(data = filter(df_m, variable != "var5"), stat = "identity", 
colour="black") + scale_fill_hue(c=45, l=80) +
  geom_line(data = filter(df_m, variable == "var5"), aes(col = variable, 
group = variable), colour="darkblue",size=1.3)

The problem is that the legend shows var5 as bar and not as a separate line. What could be the issue?

You can subset your data within the geoms, eg

ggplot(df_m) +
geom_col(aes(x=date_yq, y=value, fill=variable), data=~filter(., variable != "var5")) +
geom_line(aes(x=date_yq, y=value), colour="black", data=~filter(., variable == "var5")) +
labs(x="Date", y="Value")

hint: if you precalculate the heights of the bars, you simply use geom_col().

You need to specify the different data sets to map the geom_line from the geom_bar . Here is one way of doing that:

ggplot() +
  geom_col(data=df_m[df_m$variable != "var5",], aes(x=date_yq, y=value, fill=variable)) +
  geom_line(data=df_m[df_m$variable == "var5",], aes(x=date_yq, y=value, color=variable), lwd=1)+
  guides(fill = guide_legend(title = "Individuals")) +
  scale_color_manual(values = "darkblue", name="Grand Total") +
  xlab("Date") + 
  ylab("Value")

The initial dataset used is all the variables except for "var5", then in geom_line I defined a second dataset where it is just "var5".
Also, by using different aesthetics, "fill" and "color" I was able to separate the legends.

在此处输入图像描述

This code does what you requested

fig <- ggplot2:::ggplot(df_m, aes(x=date_yq, y=value, colour=variable)) +
  geom_line(data = subset(df_m, variable %in% c("var1", "var2", "var3", "var4"))) +
  geom_bar(data = subset(df_m, variable %in% c("var5")), stat = "identity") +
  xlab("Date") + 
  ylab("Value")

在此处输入图像描述

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