Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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 将箭头添加到刻面ggplot,在plot外部,仅在一个刻面上_R_Ggplot2 - Fatal编程技术网

R 将箭头添加到刻面ggplot,在plot外部,仅在一个刻面上

R 将箭头添加到刻面ggplot,在plot外部,仅在一个刻面上,r,ggplot2,R,Ggplot2,我的情节现在看起来像这样 我想在Y轴的左边有一个箭头指向Y轴,还有箭头旁边的文本 以下是我目前的代码: co2_diff_2050 %>% ggplot( aes( Year, MTC, fill = Sector ) ) + geom_bar( position = "stack", stat = "identity" ) + scale_fill_manual( values = CO2_sector_color ) +

我的情节现在看起来像这样

我想在Y轴的左边有一个箭头指向Y轴,还有箭头旁边的文本

以下是我目前的代码:

co2_diff_2050 %>%   
  ggplot( aes( Year, MTC, fill = Sector ) ) +
  geom_bar( position = "stack", stat = "identity" ) +
  scale_fill_manual( values = CO2_sector_color ) +
  scale_x_continuous( name = "Year", breaks = c( 2050 ) ) +
  scale_y_continuous( name = "MTC", breaks = c( 0, 5, 10 ), limits = c( -1, 13 ) ) +
  geom_segment(aes(x = 2049.5, y = maryland_80goal, xend = 2049.8, yend = maryland_80goal),
            arrow = arrow(length = unit(0.5, "cm"))) +
  geom_errorbar( aes( ymin = net_diff, ymax = net_diff ), color = "gray30", linetype = "longdash", width = 1, size = 1 ) +
  geom_hline( yintercept = 0 ) +
  theme_bw() +
  guides( fill = guide_legend( ncol = 1 ) ) +
  theme( legend.position = "right" ) +
  labs( title = "Maryland Remaining CO2 Emissions by Sector in 2050",
        caption = "Arrow indicates Maryland's 2050 GHG emissions reduction goal
        Dashed gray line indicates emissions with biomass offset") +
  facet_wrap( ~ Scenario, nrow = 1 )

这是一个有点老套的解决方案,但似乎有效。我们制作了一个
geom_segment()
,没有任何贴图美学,因此天平不会在这些方面进行训练。但是,我们确实添加了
data
参数,只是为了指示箭头应该出现在哪个面板旁边。现在通常情况下,这不会出现,因为它超出了范围,但我们可以设置
clip=“off”
来显示它

库(ggplot2)
ggplot(mpg、aes(显示、硬件))+
几何点()+
geom_段(
#更改0和25以适合您的数据
x=0,xend=-Inf,y=25,yend=25,
箭头=箭头(长度=单位(5,“pt”),
#添加适合您的数据的facetting变量
数据=数据帧(气缸=4)
) +
坐标笛卡尔(clip=“off”)+
端面缠绕(~cyl,ncol=4)


由(v0.3.0)于2020年12月3日创建,这是一种黑客解决方案,但似乎有效。我们制作了一个
geom_segment()
,没有任何贴图美学,因此天平不会在这些方面进行训练。但是,我们确实添加了
data
参数,只是为了指示箭头应该出现在哪个面板旁边。现在通常情况下,这不会出现,因为它超出了范围,但我们可以设置
clip=“off”
来显示它

库(ggplot2)
ggplot(mpg、aes(显示、硬件))+
几何点()+
geom_段(
#更改0和25以适合您的数据
x=0,xend=-Inf,y=25,yend=25,
箭头=箭头(长度=单位(5,“pt”),
#添加适合您的数据的facetting变量
数据=数据帧(气缸=4)
) +
坐标笛卡尔(clip=“off”)+
端面缠绕(~cyl,ncol=4)


由(v0.3.0)于2020-12-03创建。

您的意思是箭头应位于绘图的外部/左侧,并指向y轴?@zoowalk正确!我将添加一张我瞄准的图片。你的意思是箭头应该位于绘图的外部/左侧,并指向y轴?@zoowark正确!我要加一张我瞄准汉克斯的照片!一个问题。对于
data=data.frame(cyl=4)
我尝试了各种面板指示器,例如
data=data.frame(Scenario==“highEV”)
data=data.frame(Scenario=highEV)
,我在FUN中得到错误(X[[I]],…):找不到对象“扇区”。我的解释错了吗?当场景为highEV时,我希望箭头位于第一个面板旁边。
data=data.frame(Scenario=“highEV”)
听起来很正确。我怀疑该错误可能是由于继承的美学,因此您可能希望将
inherit.aes=FALSE
添加到段中。或者,您也可以将
fill=Sector
规范移动到
geom_-bar(aes(fill=Sector),…)
,这样它就不会干扰下游。这些我都没试过。谢谢!一个问题。对于
data=data.frame(cyl=4)
我尝试了各种面板指示器,例如
data=data.frame(Scenario==“highEV”)
data=data.frame(Scenario=highEV)
,我在FUN中得到错误(X[[I]],…):找不到对象“扇区”。我的解释错了吗?当场景为highEV时,我希望箭头位于第一个面板旁边。
data=data.frame(Scenario=“highEV”)
听起来很正确。我怀疑该错误可能是由于继承的美学,因此您可能希望将
inherit.aes=FALSE
添加到段中。或者,您也可以将
fill=Sector
规范移动到
geom_-bar(aes(fill=Sector),…)
,这样它就不会干扰下游。这些我都没有测试过。