根据显示在轴标记位置的数据对ggplot2轴标记进行着色

时间:2021-08-07 12:38:16

I have this dataset:

我有这个数据集:

top_bot_5_both <- structure(list(
  name = structure(c(20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 
                     11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), 
                   .Label = c("Michele Bachmann", "Donald Trump", "Ted Cruz", 
                              "Newt Gingrich", "Rick Santorum", "Terry McAuliffe", 
                              "Nancy Pelosi", "Debbie Wasserman Schultz", 
                              "Tammy Baldwin", "Joe Biden", "Rand Paul", "Jeb Bush", 
                              "John Kasich", "Barack Obama", "Bill Clinton", 
                              "Hillary Clinton", "Nathan Deal", "Tim Kaine", 
                              "Rob Portman", "Sherrod Brown"), 
                   class = "factor"),
  Party = c("Democratic", "Republican", "Democratic", "Republican", "Democratic", 
            "Democratic", "Democratic", "Republican", "Republican", "Republican", 
            "Republican", "Republican", "Republican", "Republican", "Republican", 
            "Democratic", "Democratic", "Democratic", "Democratic", "Democratic"), 
  total_ratings = c(35L, 48L, 51L, 49L, 296L, 41L, 599L, 64L, 80L, 55L, 
                    61L, 472L, 123L, 82L, 61L, 31L, 35L, 48L, 33L, 75L), 
  sum = c(22, 29, 21, 18, 96, 12, 172, 16, 18, 2, -86, -525, -94, -57, 
          -42, -19, -14, -7, -4, -1), 
  score = c(0.628571428571429, 0.604166666666667, 0.411764705882353, 
            0.36734693877551, 0.324324324324324, 0.292682926829268, 
            0.287145242070117, 0.25, 0.225, 0.0363636363636364, -1.40983606557377, 
            -1.11228813559322, -0.764227642276423, -0.695121951219512, 
            -0.688524590163934, -0.612903225806452, -0.4, -0.145833333333333, 
            -0.121212121212121, -0.0133333333333333)), 
  class = c("tbl_df", "tbl", "data.frame"), 
  row.names = c(NA, -20L), 
  .Names = c("name", "Party", "total_ratings", "sum", "score"))

I want to make a column graph that has the y-axis text mirror the fill color in the graph itself. Following the suggestion from this thread, I added the column color to my tibble

我想做一个列图它的y轴文本镜像了图本身的填充颜色。按照这个线程的建议,我将列颜色添加到我的tibble中

top_bot_5_both <- top_bot_5_both %>%
  mutate(color = ifelse(Party == "Democratic", "#1A80C4", "#CC3D3D"))

And then graphed it as such:

然后把它画成这样:

ggplot(top_bot_5_both, aes(x = name, y = score, fill = Party)) +
  geom_col(size = 1, color = "black") +
  coord_flip() +
  geom_vline(xintercept = 10.5, size = 1.5, linetype = "twodash") +
  scale_fill_manual(values = c("Democratic" = "#1A80C4", "Republican" = "#CC3D3D")) +
  theme(axis.text = element_text(size = 12),
    axis.text.y = element_text(color = top_bot_5_both$color),
    axis.title = element_text(size = 14),
    legend.position = "bottom", 
    legend.title = element_blank())

根据显示在轴标记位置的数据对ggplot2轴标记进行着色

In the graph, the fill correctly identifies each politician's party but the axis.text.y does not. For instance, Michelle Bachmann is a Republican, so her column should have red fill (which it does) and should have red axis text (which it does not).

在图中,填充正确地标识了每个政治家的政党,但是轴对称。y不。例如,米歇尔•巴赫曼(Michelle Bachmann)是共和党人,所以她的专栏应该有red fill(红色填充),应该有red axis文本(不是)。

I then decided to wrap it in rev() by using axis.text.y = element_text(color = rev(top_bot_5_both$color) which created this graph, which is closer to correct.

然后我决定用rev()来封装它,方法是使用axis.text。创建这个图形的y = element_text(color = rev(top_bot_5_both$color)),更接近正确。

ggplot(top_bot_5_both, aes(x = name, y = score, fill = Party)) +
  geom_col(size = 1, color = "black") +
  coord_flip() +
  geom_vline(xintercept = 10.5, size = 1.5, linetype = "twodash") +
  scale_fill_manual(values = c("Democratic" = "#1A80C4", "Republican" = "#CC3D3D")) +
  theme(axis.text = element_text(size = 12),
    axis.text.y = element_text(color = rev(top_bot_5_both$color)),
    axis.title = element_text(size = 14),
    legend.position = "bottom", 
    legend.title = element_blank())

根据显示在轴标记位置的数据对ggplot2轴标记进行着色

The top_bot_5_both$color vector is in the correct order in the tibble, but it somehow gets messed up during the actual graphing. Maybe it has to do with top_bot_5_both$name being a factor, but that is necessary I believe in order to graph the scores in descending order.

top_bot_5_both$color vector的顺序在tibble中都是正确的,但是在实际的图形绘制过程中,它不知怎么搞砸了。也许它与top_bot_5_有关,这两个名称都是一个因素,但我认为这是必要的,以便按降序对分数进行图形化。

Other things I tried that did not work:

其他我尝试过但没有成功的事情:

  • Getting rid of coord_flip() and changing y to x, etc.
  • 去掉coord_flip()并将y改为x,等等。
  • Not generating the column top_bot_5_both$color and instead putting the ifelse(...) command inside element_text(color = ...)
  • 不生成top_bot_5_$ color列,而是将ifelse(…)命令放在element_text(color =…)中。
  • And I can't do this manually because I plan to repeat this analysis in the future and the politicians' order on the y-axis will likely change
  • 我不能这么做,因为我计划在未来重复这一分析,而政客们在y轴上的顺序可能会改变。

Am I doing something wrong or is there a bug in here somewhere?

是我做错了什么事,还是有什么问题?

1 个解决方案

#1


2  

It's important to realize that when you're writing axis.text.y = element_text(color = ...), you're not mapping data to colors using the regular ggplot2 mapping mechanism, you're manually assigning colors to specific axis labels. Therefore, the colors you assign need to be exactly in the right order.

重要的是要意识到,当你写axis.text的时候。y = element_text(color =…)不是使用常规的ggplot2映射机制将数据映射到颜色,而是手工为特定的轴标签分配颜色。因此,您分配的颜色必须完全按照正确的顺序。

When you inspect your data frame, you can see that the rows are not in the order in which they are plotted; the order is set by the levels of the name factor. Therefore, you have to use that order for your colors as well. You can do that using the order() function:

当你检查你的数据帧时,你会发现这些行不是按照它们被绘制的顺序;顺序是由名称因子的级别设置的。因此,你也必须使用这个顺序来为你的颜色。可以使用order()函数:

colors <- top_bot_5_both$color[order(top_bot_5_both$name)]

Now, with this color vector, we can make the plot as intended:

现在,有了这个颜色向量,我们就可以把图画出来了:

ggplot(top_bot_5_both, aes(x = name, y = score, fill = Party)) +
  geom_col(size = 1, color = "black") +
  coord_flip() +
  geom_vline(xintercept = 10.5, size = 1.5, linetype = "twodash") +
  scale_fill_manual(values = c("Democratic" = "#1A80C4", "Republican" = "#CC3D3D")) +
  theme(axis.text = element_text(size = 12),
        axis.text.y = element_text(color = colors),
        axis.title = element_text(size = 14),
        legend.position = "bottom", 
        legend.title = element_blank())

根据显示在轴标记位置的数据对ggplot2轴标记进行着色

#1


2  

It's important to realize that when you're writing axis.text.y = element_text(color = ...), you're not mapping data to colors using the regular ggplot2 mapping mechanism, you're manually assigning colors to specific axis labels. Therefore, the colors you assign need to be exactly in the right order.

重要的是要意识到,当你写axis.text的时候。y = element_text(color =…)不是使用常规的ggplot2映射机制将数据映射到颜色,而是手工为特定的轴标签分配颜色。因此,您分配的颜色必须完全按照正确的顺序。

When you inspect your data frame, you can see that the rows are not in the order in which they are plotted; the order is set by the levels of the name factor. Therefore, you have to use that order for your colors as well. You can do that using the order() function:

当你检查你的数据帧时,你会发现这些行不是按照它们被绘制的顺序;顺序是由名称因子的级别设置的。因此,你也必须使用这个顺序来为你的颜色。可以使用order()函数:

colors <- top_bot_5_both$color[order(top_bot_5_both$name)]

Now, with this color vector, we can make the plot as intended:

现在,有了这个颜色向量,我们就可以把图画出来了:

ggplot(top_bot_5_both, aes(x = name, y = score, fill = Party)) +
  geom_col(size = 1, color = "black") +
  coord_flip() +
  geom_vline(xintercept = 10.5, size = 1.5, linetype = "twodash") +
  scale_fill_manual(values = c("Democratic" = "#1A80C4", "Republican" = "#CC3D3D")) +
  theme(axis.text = element_text(size = 12),
        axis.text.y = element_text(color = colors),
        axis.title = element_text(size = 14),
        legend.position = "bottom", 
        legend.title = element_blank())

根据显示在轴标记位置的数据对ggplot2轴标记进行着色