ggplot的因子排序不起作用。

huangapple go评论63阅读模式
英文:

ordering of ggplot not working with factors

问题

我已经按照点图的顺序创建了一个向量,但它没有按照那个顺序绘制。谢谢你的建议。

```r
order <- sav %>%
  filter(Subject == "Food") %>%
  arrange(desc(Percentage)) %>%
  select(Location) %>%
  unlist() %>%
  unname()

order <- replace(order, c(1, 8), order[c(8, 1)])

sav %>%
  ggplot(aes(x = factor(Location, levels = order), y = Percentage,
             color = Subject))+
  geom_point(data = filter(sav, Location != "IRELAND"),
             size = 4, position = position_dodge(0.5))+
  geom_point(data = filter(sav, Location == "IRELAND"),
             size = 6, position = position_dodge(1))+
  geom_linerange(data = filter(sav, Location == "IRELAND"),
                 aes(ymin = 0, ymax = Percentage), 
                 position = position_dodge(1),
                 linetype = "dotdash") + 
  geom_linerange(data = filter(sav, Location != "IRELAND"),
                 aes(ymin = 0, ymax = Percentage),
                 position = position_dodge(0.5), linetype = "dotdash") + 
  coord_flip()+
  ggtitle(label = "爱尔兰的通货膨胀率增长(按照CPI计算)与OECD和其他OECD国家相比")+
  xlab("国家 --> ")+
  ylab("CPI增长率 % -->")+
  scale_y_continuous(breaks = round(seq(0, 20, by = 1),1))+
  scale_color_manual(name = "物品类型", 
                    labels = c("食品", "总计", "不含食品和能源"),
                    values=c(unname(colorblind_colors[2]), 
                              unname(colorblind_colors[3]), 
                              unname(colorblind_colors[4])))+
  theme(panel.grid.major.x = element_line(linewidth =.01, color="black"),
        panel.grid.major.y = element_blank(),
        legend.position = "top"
    )

ggplot的因子排序不起作用。

> dput(order)
c("IRELAND", "NETHERLANDS", "SPAIN", "OECD", "ITALY", "FRANCE", 
"UNITED STATES", "GERMANY", "CANADA")

> dput(sav)
structure(list(Location = c("CANADA", "CANADA", "FRANCE", "FRANCE", 
"GERMANY", "GERMANY", "IRELAND", "IRELAND", "ITALY", "ITALY", 
"NETHERLANDS", "NETHERLANDS", "SPAIN", "SPAIN", "UNITED STATES", 
"UNITED STATES", "OECD", "OECD", "CANADA", "ITALY", "SPAIN", 
"FRANCE", "IRELAND", "UNITED STATES", "NETHERLANDS", "OECD", 
"GERMANY"), Subject = c("食品", "总计", "食品", "总计", "食品", 
"总计", "食品", "总计", "食品", "总计", "食品", "总计", "食品", 
"总计", "食品", "总计", "食品", "总计", "总计-食品和能源", "总计-食品和能源", 
"总计-食品和能源", "总计-食品和能源", "总计-食品和能源", "总计-食品和能源", 
"总计-食品和能源", "总计-食品和能源", "总计-食品和能源"), Frequency = c("每月", 
"每月", "每月", "每月", "每月", "每月", "每月", "每月", "每月", "每月", 
"每月", "每月", "每月", "每月", "每月", "每月", "每月", "每月", "每月", 
"每月", "每月", "每月", "每月", "每月", "每月", "每月", "每月"), Time = c("2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12", "2022-12", "2022-12", "2022-12", 
"2022-12", "2022-12", "2022-12"), Percentage = c(11.02015, 6.319445, 
12.86678, 5.850718, 19.75631, 8.550855, 11.74636, 8.224299, 
13.14815, 11.63227, 16.7983, 9.586879, 15.68565, 5.70769, 11.88275, 
6.454401, 15.60381, 9.438622, 5.58275, 4.469475, 4.442303, 3.36004, 
4.999758, 5.707835, 6.034873, 7.221961, 5.05511)), class = "data.frame", row.names = c(NA, -27L))
英文:

I have created a vector with the order of a dot plot mentioned but it doesn't plot in that order/ Thanks for the suggestions.

order &lt;- sav %&gt;%
  filter(Subject == &quot;Food&quot;) %&gt;%
  arrange(desc(Percentage)) %&gt;%
  select(Location) %&gt;% 
  unlist() %&gt;%
  unname()

order &lt;- replace(order, c(1, 8), order[c(8, 1)])

sav %&gt;%
  ggplot(aes(x = factor(Location, levels = order), y = Percentage,
             color = Subject))+
  geom_point(data = filter(sav, Location != &quot;IRELAND&quot;),
             size = 4, position = position_dodge(0.5))+
  geom_point(data = filter(sav, Location == &quot;IRELAND&quot;),
             size = 6, position = position_dodge(1))+
  geom_linerange(data = filter(sav, Location == &quot;IRELAND&quot;),
                 aes(ymin = 0, ymax = Percentage), 
                 position = position_dodge(1),
                linetype = &quot;dotdash&quot;) + 
  geom_linerange(data = filter(sav, Location != &quot;IRELAND&quot;),
                aes(ymin = 0, ymax = Percentage),
                 position = position_dodge(0.5), linetype = &quot;dotdash&quot;) + 
  coord_flip()+
  ggtitle(label = &quot;Increase in inflation (by CPI) in Ireland compared to OECD and other countries in OECD&quot;)+
  xlab(&quot;Countries --&gt; &quot;) +
  ylab(&quot;Increase in CPI by % --&gt;&quot;)+
  scale_y_continuous(breaks = round(seq(0, 20, by = 1),1))+
  scale_color_manual(name = &quot;Type of Items&quot;, 
                    labels = c(&quot;Food&quot;, &quot;Total&quot;, &quot;Excluding food and energy&quot;),
                    values=c(unname(colorblind_colors[2]), 
                              unname(colorblind_colors[3]), 
                              unname(colorblind_colors[4])))+
  theme(panel.grid.major.x = element_line(linewidth =.01, color=&quot;black&quot;),
        panel.grid.major.y = element_blank(),
        legend.position = &quot;top&quot;
    )

ggplot的因子排序不起作用。

&gt; dput(order)
c(&quot;IRELAND&quot;, &quot;NETHERLANDS&quot;, &quot;SPAIN&quot;, &quot;OECD&quot;, &quot;ITALY&quot;, &quot;FRANCE&quot;, 
&quot;UNITED STATES&quot;, &quot;GERMANY&quot;, &quot;CANADA&quot;)

&gt; dput(sav)
structure(list(Location = c(&quot;CANADA&quot;, &quot;CANADA&quot;, &quot;FRANCE&quot;, &quot;FRANCE&quot;, 
&quot;GERMANY&quot;, &quot;GERMANY&quot;, &quot;IRELAND&quot;, &quot;IRELAND&quot;, &quot;ITALY&quot;, &quot;ITALY&quot;, 
&quot;NETHERLANDS&quot;, &quot;NETHERLANDS&quot;, &quot;SPAIN&quot;, &quot;SPAIN&quot;, &quot;UNITED STATES&quot;, 
&quot;UNITED STATES&quot;, &quot;OECD&quot;, &quot;OECD&quot;, &quot;CANADA&quot;, &quot;ITALY&quot;, &quot;SPAIN&quot;, 
&quot;FRANCE&quot;, &quot;IRELAND&quot;, &quot;UNITED STATES&quot;, &quot;NETHERLANDS&quot;, &quot;OECD&quot;, 
&quot;GERMANY&quot;), Subject = c(&quot;Food&quot;, &quot;Total&quot;, &quot;Food&quot;, &quot;Total&quot;, &quot;Food&quot;, 
&quot;Total&quot;, &quot;Food&quot;, &quot;Total&quot;, &quot;Food&quot;, &quot;Total&quot;, &quot;Food&quot;, &quot;Total&quot;, &quot;Food&quot;, 
&quot;Total&quot;, &quot;Food&quot;, &quot;Total&quot;, &quot;Food&quot;, &quot;Total&quot;, &quot;Total_Minus_Food_Energy&quot;, 
&quot;Total_Minus_Food_Energy&quot;, &quot;Total_Minus_Food_Energy&quot;, &quot;Total_Minus_Food_Energy&quot;, 
&quot;Total_Minus_Food_Energy&quot;, &quot;Total_Minus_Food_Energy&quot;, &quot;Total_Minus_Food_Energy&quot;, 
&quot;Total_Minus_Food_Energy&quot;, &quot;Total_Minus_Food_Energy&quot;), Frequency = c(&quot;Monthly&quot;, 
&quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, 
&quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, 
&quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, 
&quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, &quot;Monthly&quot;, 
&quot;Monthly&quot;, &quot;Monthly&quot;), Time = c(&quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, 
&quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, 
&quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, 
&quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, 
&quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;, &quot;2022-12&quot;
), Percentage = c(11.02015, 6.319445, 12.86678, 5.850718, 19.75631, 
8.550855, 11.74636, 8.224299, 13.14815, 11.63227, 16.7983, 9.586879, 
15.68565, 5.70769, 11.88275, 6.454401, 15.60381, 9.438622, 5.58275, 
4.469475, 4.442303, 3.36004, 4.999758, 5.707835, 6.034873, 7.221961, 
5.05511)), class = &quot;data.frame&quot;, row.names = c(NA, -27L))

答案1

得分: 2

几件事情:

  1. factors 需要在 ggplot 每个几何图形中看到的数据中,但是在第一个映射中,虽然你在设置 factor(Location,levels=order),但是 data= 参数没有使用相同的数据。

    对于这个问题,我通常更喜欢在前期将数据 factor 化,并使用 ~ 样式的 "函数" 来设置 data=

  2. 不确定为什么会出现这种情况,但是它仍然不起作用... 但是如果第一个 geom_point 调用使用 mutate 并将 IRELAND 的百分比值替换为 NA,则级别会保留。奇怪。

### 我仍然没有这些 :-)
colorblind_colors <- 1:4

sav %>%
  mutate(Location = factor(Location, levels = order)) %>%
  ggplot(aes(x = Location, y = Percentage, color = Subject)) +
  geom_point(data = ~ mutate(., Percentage = if_else(Location == "Ireland", Percentage[NA], Percentage)),
             size = 4, position = position_dodge(0.5), na.rm = TRUE) +
  geom_point(data = ~ filter(., Location == "IRELAND"),
             size = 6, position = position_dodge(1)) +
  geom_linerange(data = ~ filter(., Location == "IRELAND"),
                 aes(ymin = 0, ymax = Percentage), 
                 position = position_dodge(1),
                 linetype = "dotdash") + 
  geom_linerange(data = ~ filter(., Location != "IRELAND"),
                 aes(ymin = 0, ymax = Percentage),
                 position = position_dodge(0.5), linetype = "dotdash") + 
  coord_flip() +
  ggtitle(label = "爱尔兰通胀增长(按CPI)与OECD和其他OECD国家的比较") +
  xlab("国家 --> ") +
  ylab("CPI增长(百分比) -->") +
  scale_y_continuous(breaks = round(seq(0, 20, by = 1), 1)) +
  scale_color_manual(name = "物品类型", 
                     labels = c("食品", "总计", "除食品和能源外"),
                     values=c(unname(colorblind_colors[2]), 
                              unname(colorblind_colors[3]), 
                              unname(colorblind_colors[4]))) +
  theme(panel.grid.major.x = element_line(linewidth = .01, color = "black"),
        panel.grid.major.y = element_blank(),
        legend.position = "top")

ggplot的因子排序不起作用。

英文:

A couple of things:

  1. The factors need to be in the data that ggplot sees at every geom, but while you're setting factor(Location,levels=order) in the first mapping, none of the data= arguments is using the same data.

    For this, I generally prefer factorizing the data up-front, and using ~-style "functions" for data=.

  2. Not sure exactly why this is the case, but it still doesn't work ... but if the first call to geom_point uses mutate and replaces IRELAND's percentage values with NA, the levels are retained. Weird.

### I still don&#39;t have these :-)
colorblind_colors &lt;- 1:4

sav %&gt;%
  mutate(Location = factor(Location, levels = order)) %&gt;%
  ggplot(aes(x = Location, y = Percentage, color = Subject)) +
  geom_point(data = ~ mutate(., Percentage = if_else(Location == &quot;Ireland&quot;, Percentage[NA], Percentage)),
             size = 4, position = position_dodge(0.5), na.rm = TRUE) +
  geom_point(data = ~ filter(., Location == &quot;IRELAND&quot;),
             size = 6, position = position_dodge(1)) +
  geom_linerange(data = ~ filter(., Location == &quot;IRELAND&quot;),
                 aes(ymin = 0, ymax = Percentage), 
                 position = position_dodge(1),
                 linetype = &quot;dotdash&quot;) + 
  geom_linerange(data = ~ filter(., Location != &quot;IRELAND&quot;),
                 aes(ymin = 0, ymax = Percentage),
                 position = position_dodge(0.5), linetype = &quot;dotdash&quot;) + 
  coord_flip()+
  ggtitle(label = &quot;Increase in inflation (by CPI) in Ireland compared to OECD and other countries in OECD&quot;)+
  xlab(&quot;Countries --&gt; &quot;) +
  ylab(&quot;Increase in CPI by % --&gt;&quot;)+
  scale_y_continuous(breaks = round(seq(0, 20, by = 1),1))+
  scale_color_manual(name = &quot;Type of Items&quot;, 
                     labels = c(&quot;Food&quot;, &quot;Total&quot;, &quot;Excluding food and energy&quot;),
                     values=c(unname(colorblind_colors[2]), 
                              unname(colorblind_colors[3]), 
                              unname(colorblind_colors[4])))+
  theme(panel.grid.major.x = element_line(linewidth =.01, color=&quot;black&quot;),
        panel.grid.major.y = element_blank(),
        legend.position = &quot;top&quot;
        )

ggplot的因子排序不起作用。

huangapple
  • 本文由 发表于 2023年2月14日 04:26:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定