简体   繁体   English

如何使用 R 为特定变量组合折线图和条形图

[英]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.我会为 var1、var2、var3 和 var4 绘制条形图,为 var5 绘制折线图。 Line and bars should be in the same chart.线和条应该在同一个图表中。 How can I get it keeping the long format with ggplot?我怎样才能让它保持与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.问题是图例将 var5 显示为 bar 而不是单独的行。 What could be the issue?可能是什么问题?

You can subset your data within the geoms, eg您可以在 geoms 中对数据进行子集化,例如

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().提示:如果您预先计算了条形的高度,您只需使用 geom_col()。

You need to specify the different data sets to map the geom_line from the geom_bar .您需要从geom_bar geom_line 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".使用的初始数据集是除“var5”之外的所有变量,然后在 geom_line 我定义了第二个数据集,它只是“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")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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