在GGPLOT中替换X轴tick和具有百分位等级的文本

发布于 2025-01-31 17:45:21 字数 1178 浏览 5 评论 0原文

我目前正在尝试复制以下图: 要复制的情节

我正在用代替x-axis tick ticks和文本是百分位等级而不是国家ISO(本质上是在“要复制的情节”中复制x轴)

我目前在此图像中处于舞台上:尝试复制

我正在使用以下代码:

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") 
  )

i i尝试使用具有各种输入的选项,但我似乎可以使其起作用:

  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)

对于那些还可以告诉我如何在正确点上添加某些特定国家名称的人,像“要复制”中的情节一样。

非常感谢您的帮助!

I am currently trying to reproduce the following graph:
Plot to be replicated

What I am struggling with is substituting the x-axis ticks and text to be the percentile rank rather than the country ISO (essentially replicating the x-axis in the "Plot to be replicated").

I am currently at the stage in this image: Attempted Replication

I am using the following code:

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") 
  )

I tried to use the options with various inputs but I can seem to make it work:

  theme(axis.ticks.x = ,
        axis.text.x =) 
  )

Some data useful for your replication:

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)

Extra points for those who can also tell me how to add certain specific country names at the right point inside of the plot like in the "Plot to be replicated".

Thank you so much for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2025-02-07 17:45:21

包括选择性国家标签:

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")
  )

“”

在2022-05-24创建的 reprex软件包(v2.0.1)

Includes selective country labelling:

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")
  )

Created on 2022-05-24 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文