Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 studio中使用ggplot组合条形图中的两个变量?_R_Variables_Ggplot2_Bar Chart - Fatal编程技术网

如何在R studio中使用ggplot组合条形图中的两个变量?

如何在R studio中使用ggplot组合条形图中的两个变量?,r,variables,ggplot2,bar-chart,R,Variables,Ggplot2,Bar Chart,我试图用ggplot绘制一个多元条形图,这样一个条形图由两种不同的颜色组成,代表两个变量,而不是并排比较这两个变量。这两个变量是正概率(P.probability)和负概率(N.probability) 下面的代码是我的数据。我只能在代码中包含P.Probability,但我希望P.Probability和N.Probability都包含在我上面提到的图表中 Month <- c("Aug", "Sep", "Oct") P.Probability <- c(0.5, 0.6, 0.

我试图用ggplot绘制一个多元条形图,这样一个条形图由两种不同的颜色组成,代表两个变量,而不是并排比较这两个变量。这两个变量是正概率(
P.probability
)和负概率(
N.probability

下面的代码是我的数据。我只能在代码中包含
P.Probability
,但我希望
P.Probability
N.Probability
都包含在我上面提到的图表中

Month <- c("Aug", "Sep", "Oct")
P.Probability <- c(0.5, 0.6, 0.6)
N.Probability <- 1-P.Probability

dtf2 <- data.frame(Month, P.Probability, N.Probability)

ggplot(dtf2, aes(x = Month, y = P.Probability)) + 
  geom_bar(stat = "identity") + 
  geom_hline(yintercept=0)+
  theme(axis.title.y=element_blank(),
        axis.title.x=element_blank(),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))+
  theme(panel.background = element_blank(),
        axis.ticks.x = element_blank()) +
  theme(axis.line.y = element_line(color="black", size = 0.5))+
  geom_text(aes(label = paste(P.Probability*100, "%"), vjust = ifelse(P.Probability >= 0, -0.5, 1.2)))+
  scale_y_continuous(labels=scales::percent)

Month我建议收集成长格式,以便您可以映射填充颜色的概率类型

dtf2_long <- tidyr::gather(dtf2, type, Probability, -Month)

ggplot(dtf2_long, aes(x = Month, y = Probability, fill = type)) + 
  geom_bar(stat = "identity") + 
  geom_hline(yintercept=0)+
  theme(axis.title.y=element_blank(),
        axis.title.x=element_blank(),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))+
  theme(panel.background = element_blank(),
        axis.ticks.x = element_blank()) +
  theme(axis.line.y = element_line(color="black", size = 0.5))+
  geom_text(data = dtf2_long %>% filter(type == "P.Probability"),
            aes(label = paste(Probability*100, "%"), vjust = ifelse(Probability >= 0, -0.5, 1.2)))+
  scale_y_continuous(labels=scales::percent)
dtf2_long%过滤器(类型==“P.Probability”),
aes(标签=粘贴(概率*100,“%”,vjust=ifelse(概率>=0,-0.5,1.2)))+
比例y连续(标签=比例::百分比)

我建议收集成长格式,以便您可以映射填充颜色的概率类型

dtf2_long <- tidyr::gather(dtf2, type, Probability, -Month)

ggplot(dtf2_long, aes(x = Month, y = Probability, fill = type)) + 
  geom_bar(stat = "identity") + 
  geom_hline(yintercept=0)+
  theme(axis.title.y=element_blank(),
        axis.title.x=element_blank(),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))+
  theme(panel.background = element_blank(),
        axis.ticks.x = element_blank()) +
  theme(axis.line.y = element_line(color="black", size = 0.5))+
  geom_text(data = dtf2_long %>% filter(type == "P.Probability"),
            aes(label = paste(Probability*100, "%"), vjust = ifelse(Probability >= 0, -0.5, 1.2)))+
  scale_y_continuous(labels=scales::percent)
dtf2_long%过滤器(类型==“P.Probability”),
aes(标签=粘贴(概率*100,“%”,vjust=ifelse(概率>=0,-0.5,1.2)))+
比例y连续(标签=比例::百分比)

谢谢您的回复!它对我有用:)然而,我应该怎么做才能使N.概率在底部,P.概率在顶部,你所显示的图形看起来像是我想要的那样颠倒。此外,是否可以改变颜色,使N.概率为黑色,P.概率为红色?谢谢谢谢你的回复!它对我有用:)然而,我应该怎么做才能使N.概率在底部,P.概率在顶部,你所显示的图形看起来像是我想要的那样颠倒。此外,是否可以改变颜色,使N.概率为黑色,P.概率为红色?谢谢