在ggplot中用百分位等级替换X轴刻度和文本
Substituting X-axis ticks and text with percentile rank in ggplot
我目前正在尝试重现下图:
Plot to be replicated
我正在努力的是将 x 轴刻度和文本替换为百分位排名而不是国家 ISO(本质上是复制“要复制的图”中的 x 轴) .
我目前处于这张图片的阶段:Attempted Replication
我正在使用以下代码:
ggplot(data, aes(x = x, y = y)) +
geom_errorbar(aes(ymax = Cup, ymin = Cdo)) +
labs(x = "Country ISO",
y = "Transparency Rating",
title = "Transparency Index with CI") +
theme_light() +
theme(panel.grid.major.x = element_blank() ,
panel.grid.major.y = element_line( size=.05, color="grey")
)
我尝试使用具有各种输入的选项,但我似乎可以让它工作:
theme(axis.ticks.x = ,
axis.text.x =)
)
一些对您的复制有用的数据:
x<-c("AFG","ALB","ITA",
"IND","AGO","ARE",
"ATG","BEN","BFA",
"BGD","BGR","BHR",
"BHS","BIH","BRB",
"BRN","BTN","BWA",
"CAF","CAN")
y<-c(1:20)
Cup<-y+0.4
Cdo<-y-0.4
data<- data.frame(x,y,Cup,Cdo)
那些还可以告诉我如何在情节内的正确位置添加某些特定国家名称的人加分,就像在“要复制的情节”中一样。
非常感谢您的帮助!
包括选择性国家标签:
library(tidyverse)
x <- c(
"AFG", "ALB", "ITA",
"IND", "AGO", "ARE",
"ATG", "BEN", "BFA",
"BGD", "BGR", "BHR",
"BHS", "BIH", "BRB",
"BRN", "BTN", "BWA",
"CAF", "CAN"
)
y <- c(1:20)
Cup <- y + 0.4
Cdo <- y - 0.4
data <- data.frame(x, y, Cup, Cdo) |>
mutate(rank = percent_rank(y))
labels <- data |>
filter(x %in% c("ITA", "BGR", "CAN")) # select countries to label
data |>
ggplot(aes(rank, y)) +
geom_text(aes(label = x), data = labels, nudge_y = -2, angle = -90) +
geom_errorbar(aes(ymax = Cup, ymin = Cdo)) +
labs(
x = "Pct Rank of Country ISO",
y = "Transparency Rating",
title = "Transparency Index with CI"
) +
theme_light() +
theme(
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(size = .05, color = "grey")
)
由 reprex package (v2.0.1)
于 2022-05-24 创建
我目前正在尝试重现下图: Plot to be replicated
我正在努力的是将 x 轴刻度和文本替换为百分位排名而不是国家 ISO(本质上是复制“要复制的图”中的 x 轴) .
我目前处于这张图片的阶段:Attempted Replication
我正在使用以下代码:
ggplot(data, aes(x = x, y = y)) +
geom_errorbar(aes(ymax = Cup, ymin = Cdo)) +
labs(x = "Country ISO",
y = "Transparency Rating",
title = "Transparency Index with CI") +
theme_light() +
theme(panel.grid.major.x = element_blank() ,
panel.grid.major.y = element_line( size=.05, color="grey")
)
我尝试使用具有各种输入的选项,但我似乎可以让它工作:
theme(axis.ticks.x = ,
axis.text.x =)
)
一些对您的复制有用的数据:
x<-c("AFG","ALB","ITA",
"IND","AGO","ARE",
"ATG","BEN","BFA",
"BGD","BGR","BHR",
"BHS","BIH","BRB",
"BRN","BTN","BWA",
"CAF","CAN")
y<-c(1:20)
Cup<-y+0.4
Cdo<-y-0.4
data<- data.frame(x,y,Cup,Cdo)
那些还可以告诉我如何在情节内的正确位置添加某些特定国家名称的人加分,就像在“要复制的情节”中一样。
非常感谢您的帮助!
包括选择性国家标签:
library(tidyverse)
x <- c(
"AFG", "ALB", "ITA",
"IND", "AGO", "ARE",
"ATG", "BEN", "BFA",
"BGD", "BGR", "BHR",
"BHS", "BIH", "BRB",
"BRN", "BTN", "BWA",
"CAF", "CAN"
)
y <- c(1:20)
Cup <- y + 0.4
Cdo <- y - 0.4
data <- data.frame(x, y, Cup, Cdo) |>
mutate(rank = percent_rank(y))
labels <- data |>
filter(x %in% c("ITA", "BGR", "CAN")) # select countries to label
data |>
ggplot(aes(rank, y)) +
geom_text(aes(label = x), data = labels, nudge_y = -2, angle = -90) +
geom_errorbar(aes(ymax = Cup, ymin = Cdo)) +
labs(
x = "Pct Rank of Country ISO",
y = "Transparency Rating",
title = "Transparency Index with CI"
) +
theme_light() +
theme(
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(size = .05, color = "grey")
)
由 reprex package (v2.0.1)
于 2022-05-24 创建