Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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,我有一个条形图,我还想包括一些线条,显示它们之间的百分比差异,如下图所示: 图中的线条只是为了说明我理想中想要的东西 有人能帮我吗 以下是复制该图的数据框: structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01, 0.11, 5.83, 21.4, 72.64), quantile = structure(c(4L, 1L, 2L, 3L, 5L, 4L, 1L, 2L, 3L, 5L), .Label = c("2nd

我有一个条形图,我还想包括一些线条,显示它们之间的百分比差异,如下图所示:

图中的线条只是为了说明我理想中想要的东西

有人能帮我吗

以下是复制该图的数据框:

structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01, 
0.11, 5.83, 21.4, 72.64), quantile = structure(c(4L, 1L, 2L, 
3L, 5L, 4L, 1L, 2L, 3L, 5L), .Label = c("2nd Quantile", "3rd Quantile", 
"4nd Quantile", "Poorest 20%", "Richest 20%"), class = "factor"), 
    case = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("No Debt", "With Debt"), class = "factor")), row.names = c(NA, 
-10L), class = "data.frame")
这是我用来绘制条形图的代码:

ggplot(df_cum, aes(fill = case , quantile, shares)) + geom_bar(position =
                                                                 "dodge", stat = "identity") +
  scale_x_discrete(limits = c(
    "Poorest 20%",
    "2nd Quantile",
    "3rd Quantile",
    "4nd Quantile",
    "Richest 20%"
  )) +
  theme_minimal()
您的数据保持不变:

library(tidyverse)
df_cum<-structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01,0.11, 5.83, 21.4, 72.64), 
quantile = structure(c(4L, 1L, 2L, 3L, 5L, 4L, 1L, 2L, 3L, 5L), 
.Label = c("2nd Quantile", "3rd Quantile", "4nd Quantile", "Poorest 20%", "Richest 20%"), class = "factor"), 
 case = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("No Debt", "With Debt"), class = "factor")), row.names = c(NA, -10L), class = "data.frame")
p <- ggplot(df_cum, aes(fill = case , quantile, shares)) + 
   geom_bar(position = "dodge", stat = "identity") +
   scale_x_discrete(limits = c("Poorest 20%", "2nd Quantile", "3rd Quantile", "4nd Quantile", "Richest 20%")) +
theme_minimal() 
您的图形保持不变:

library(tidyverse)
df_cum<-structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01,0.11, 5.83, 21.4, 72.64), 
quantile = structure(c(4L, 1L, 2L, 3L, 5L, 4L, 1L, 2L, 3L, 5L), 
.Label = c("2nd Quantile", "3rd Quantile", "4nd Quantile", "Poorest 20%", "Richest 20%"), class = "factor"), 
 case = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("No Debt", "With Debt"), class = "factor")), row.names = c(NA, -10L), class = "data.frame")
p <- ggplot(df_cum, aes(fill = case , quantile, shares)) + 
   geom_bar(position = "dodge", stat = "identity") +
   scale_x_discrete(limits = c("Poorest 20%", "2nd Quantile", "3rd Quantile", "4nd Quantile", "Richest 20%")) +
theme_minimal() 
我用水平误差条来做这个把戏。以下是我的解决方案:

y = rep(c(3, 5, 13, 25, 75),2)
x = rep(c(1:5), 2)
label = rep(c("-3%", "-5%", "-2%", "-1%", "10%"), 2) 
p1 <- p + geom_text(x=x, y=y+2, label=label)  
p1 + geom_errorbarh(aes(xmax = (x + 0.3), xmin = (x - 0.3), y = y), height = 0.5) 
现在,您可以:

如果愿意,您还可以调整高度和宽度。

您的数据保持不变:

library(tidyverse)
df_cum<-structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01,0.11, 5.83, 21.4, 72.64), 
quantile = structure(c(4L, 1L, 2L, 3L, 5L, 4L, 1L, 2L, 3L, 5L), 
.Label = c("2nd Quantile", "3rd Quantile", "4nd Quantile", "Poorest 20%", "Richest 20%"), class = "factor"), 
 case = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("No Debt", "With Debt"), class = "factor")), row.names = c(NA, -10L), class = "data.frame")
p <- ggplot(df_cum, aes(fill = case , quantile, shares)) + 
   geom_bar(position = "dodge", stat = "identity") +
   scale_x_discrete(limits = c("Poorest 20%", "2nd Quantile", "3rd Quantile", "4nd Quantile", "Richest 20%")) +
theme_minimal() 
您的图形保持不变:

library(tidyverse)
df_cum<-structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01,0.11, 5.83, 21.4, 72.64), 
quantile = structure(c(4L, 1L, 2L, 3L, 5L, 4L, 1L, 2L, 3L, 5L), 
.Label = c("2nd Quantile", "3rd Quantile", "4nd Quantile", "Poorest 20%", "Richest 20%"), class = "factor"), 
 case = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("No Debt", "With Debt"), class = "factor")), row.names = c(NA, -10L), class = "data.frame")
p <- ggplot(df_cum, aes(fill = case , quantile, shares)) + 
   geom_bar(position = "dodge", stat = "identity") +
   scale_x_discrete(limits = c("Poorest 20%", "2nd Quantile", "3rd Quantile", "4nd Quantile", "Richest 20%")) +
theme_minimal() 
我用水平误差条来做这个把戏。以下是我的解决方案:

y = rep(c(3, 5, 13, 25, 75),2)
x = rep(c(1:5), 2)
label = rep(c("-3%", "-5%", "-2%", "-1%", "10%"), 2) 
p1 <- p + geom_text(x=x, y=y+2, label=label)  
p1 + geom_errorbarh(aes(xmax = (x + 0.3), xmin = (x - 0.3), y = y), height = 0.5) 
现在,您可以:


如果您愿意,还可以调整高度和宽度。

太好了。如果你能帮忙,还有一件事。如何将百分比的文本字体更改为Times New Roman?我用了“text=element\u textfamily=Times New Roman”,“in theme”,但没有用,太棒了。如果你能帮忙,还有一件事。如何将百分比的文本字体更改为Times New Roman?我使用了“text=element\u textfamily=Times New Roman”,“in theme”,但没有起作用。