Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用ggplot2在单个变量上映射不同类型的几何图形_R_Ggplot2 - Fatal编程技术网

R 如何使用ggplot2在单个变量上映射不同类型的几何图形

R 如何使用ggplot2在单个变量上映射不同类型的几何图形,r,ggplot2,R,Ggplot2,我使用ggplot2绘制了此图: 我希望将“Interés Real”时间序列作为geom_bar()而不是直线。我尝试使用以下代码分别绘制我的每个系列: ggOjb=ggplot(base)+geom_line(aes(x=periodo,y=tasa))+ geom_line(aes(x=periodo,y=inflacion))+ geom_bar(aes(x=periodo,y=real2),stat="identity",alpha=.7)+ geom_hline(yin

我使用ggplot2绘制了此图:

我希望将“Interés Real”时间序列作为
geom_bar()
而不是直线。我尝试使用以下代码分别绘制我的每个系列:

ggOjb=ggplot(base)+geom_line(aes(x=periodo,y=tasa))+
  geom_line(aes(x=periodo,y=inflacion))+
  geom_bar(aes(x=periodo,y=real2),stat="identity",alpha=.7)+
  geom_hline(yintercept=0,linetype=2,size=1)
我得到了这个:

这正是我想要的。但我也想保留这些指南。因此,我想知道如何将变量映射到几何体的类型


提前谢谢你!我希望我说得很清楚。

您可以将数据从宽改为长,然后在每个geom中使用不同的数据帧

library(tidyr)
df <- data.frame(Time = 1:25, A = rnorm(25), B = rnorm(25), C = rnorm(25)*1.5)

df %>% 
  gather("Series", "Value", - Time) -> df

ggplot() + 
  geom_bar(data = df %>% filter(Series == "C"),  aes(x=Time, y=Value, fill = "SeriesA"), stat="identity") +
  geom_line(data = df %>% filter(Series == "A"), aes(x=Time, y=Value, col = "SeriesB")) +
  geom_line(data = df %>% filter(Series == "B"), aes(x=Time, y=Value, col = "SeriesC")) +
  geom_hline(yintercept=0,linetype=2,size=1) +
  scale_colour_manual(name = "Series A and B", values = c("SeriesB" = "blue", "SeriesC" = "green")) +
  scale_fill_manual(name = "Series C", values = c("SeriesA" = "grey"))
library(tidyr)
df%
聚集(“系列”、“值”、-时间)->df
ggplot()+
几何图形栏(数据=df%>%过滤器(系列=“C”),aes(x=时间,y=值,fill=“SeriesA”),stat=“identity”)+
geom_线(数据=df%>%过滤器(系列=“A”),aes(x=时间,y=值,col=“serieb”))+
geom_线(数据=df%>%过滤器(系列=“B”),aes(x=时间,y=值,col=“SeriesC”))+
geom_hline(yintercept=0,线型=2,尺寸=1)+
比例颜色手册(名称=“系列A和B”,值=c(“系列B”=“蓝色”,“系列c”=“绿色”))+
比例填充手册(名称=“系列C”,值=C(“系列A”=“灰色”))

您可以将数据从宽改为长,然后在每个geom中使用不同的数据帧

library(tidyr)
df <- data.frame(Time = 1:25, A = rnorm(25), B = rnorm(25), C = rnorm(25)*1.5)

df %>% 
  gather("Series", "Value", - Time) -> df

ggplot() + 
  geom_bar(data = df %>% filter(Series == "C"),  aes(x=Time, y=Value, fill = "SeriesA"), stat="identity") +
  geom_line(data = df %>% filter(Series == "A"), aes(x=Time, y=Value, col = "SeriesB")) +
  geom_line(data = df %>% filter(Series == "B"), aes(x=Time, y=Value, col = "SeriesC")) +
  geom_hline(yintercept=0,linetype=2,size=1) +
  scale_colour_manual(name = "Series A and B", values = c("SeriesB" = "blue", "SeriesC" = "green")) +
  scale_fill_manual(name = "Series C", values = c("SeriesA" = "grey"))
library(tidyr)
df%
聚集(“系列”、“值”、-时间)->df
ggplot()+
几何图形栏(数据=df%>%过滤器(系列=“C”),aes(x=时间,y=值,fill=“SeriesA”),stat=“identity”)+
geom_线(数据=df%>%过滤器(系列=“A”),aes(x=时间,y=值,col=“serieb”))+
geom_线(数据=df%>%过滤器(系列=“B”),aes(x=时间,y=值,col=“SeriesC”))+
geom_hline(yintercept=0,线型=2,尺寸=1)+
比例颜色手册(名称=“系列A和B”,值=c(“系列B”=“蓝色”,“系列c”=“绿色”))+
比例填充手册(名称=“系列C”,值=C(“系列A”=“灰色”))

一些示例数据可能会有帮助。你说的“保留指南”是什么意思?添加
geom_线(aes(x=periodo,y=real2))
也许吧?一些示例数据可能会有帮助。你说的“保留指南”是什么意思?添加
geom_线(aes(x=periodo,y=real2))
也许吧?谢谢,它成功了!我最初有一个很长的格式,但不知道我可以在ggplot函数中进行过滤。谢谢,它成功了!我最初有一个很长的格式,但不知道我可以在ggplot函数中进行过滤。