反转条形图颜色的顺序,同时保持图例顺序和颜色

发布于 2025-01-13 11:26:19 字数 1321 浏览 0 评论 0原文

我正在 R 中研究 ggplot2,并使用了自动着色功能。它绘制堆积条形图,然后将其转换为饼图。这是代码:

ggplot(data=reg_sub_rj_reviewed, aes(x=factor(1), stat="bin", fill=`project-effort`)) + 
  geom_bar(position="fill") + # Stacked bar chart
  facet_grid(facets=. ~ strength) + # Side by side bar chart
  coord_polar(theta="y") + # side by side pie chart
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank()
        ) +
  labs(
    x = "",
    y = "Graded Result (code quality)",
    title = "Graded Result Broken Down By Coding Effort",
    fill = "Project Effort"
  ) +
  NULL

生成的图像是: 输入图片此处描述

我希望饼图的颜色顺序与图例相同(当您以顺时针方向查看饼图时)。从 12 点钟开始顺时针旋转:第一个颜色应该是橙色,然后是棕色,然后是绿色等(当前饼图的顺时针绘图是从粉红色(最后一个值!),然后是紫色等)。

PS 另外,如何去掉图表左侧绘制的 1 ?

上述问题的可重现示例几乎适用于任何饼图,例如:

slices <- c(10, 12, 4, 16, 8) 
lbls <- c("1st", "2nd", "3rd", "4th", "5th")
pie(slices,labels = lbls, col=rainbow(length(lbls)),
   main="Pie Chart of Countries")

结果: 输入图片此处描述

从人类角度来看,我希望在图像中顺时针编号第 1 个,然后是第 2 个(而不是第 5 个),即不是逆时针编号。 (抱歉,我不知道为什么图像会这么大)。

I am working on ggplot2 in R, and have used automatic colouring for the plot. It plots stacked bar charts and then converts them to the pie chart. Here is the code:

ggplot(data=reg_sub_rj_reviewed, aes(x=factor(1), stat="bin", fill=`project-effort`)) + 
  geom_bar(position="fill") + # Stacked bar chart
  facet_grid(facets=. ~ strength) + # Side by side bar chart
  coord_polar(theta="y") + # side by side pie chart
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank()
        ) +
  labs(
    x = "",
    y = "Graded Result (code quality)",
    title = "Graded Result Broken Down By Coding Effort",
    fill = "Project Effort"
  ) +
  NULL

The resulting image is:
enter image description here

I would like the pie chart to have the same order of colours as the legend (when you look at pie chart in a clock-wise way). Start from 12 o'clock and go clockwise: first colour should be orange, then brown, then green etc (currently clockwise plot of pie chart is from the pink (last value!), then purple etc).

PS Also, how can I get rid of the 1 being plotted on the left side of the chart?

A reproducible example of the above issue, is pretty much for any pie chart, for example:

slices <- c(10, 12, 4, 16, 8) 
lbls <- c("1st", "2nd", "3rd", "4th", "5th")
pie(slices,labels = lbls, col=rainbow(length(lbls)),
   main="Pie Chart of Countries")

Result:
enter image description here

Humanly I'd expect clockwise numbering 1st, then 2nd (not 5th) in the image, i.e. not anticlockwise. (Sorry I don't know why the image is coming up so huge).

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

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

发布评论

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

评论(2

哆兒滾 2025-01-20 11:26:19
pi_df <- data.frame(slices, lbls)

library(dplyr); library(forcats)
pi_df %>%
  mutate(lbls = fct_inorder(lbls) %>% fct_rev) %>%
  ggplot(aes(x=slices, y = factor(1), fill = lbls)) +
  geom_col() +
  guides(fill = guide_legend(reverse = TRUE)) +
  coord_polar()

输入图片这里的描述

如果我们添加这些行:

... + 
geom_text(aes(label = slices), position = position_stack(vjust = 0.5)) +
theme_void() # to remove axis lines and background shading

我们可以获得每个切片内标记的值。我们必须使用 position 参数,以便 geom_text 知道我们希望文本标签在饼图周围逐渐堆叠,就像切片自动堆叠在 geom_col 中一样()vjust 表示我们希望标签位于每个切片的中间,而不是两侧边缘。

输入图片此处描述

pi_df <- data.frame(slices, lbls)

library(dplyr); library(forcats)
pi_df %>%
  mutate(lbls = fct_inorder(lbls) %>% fct_rev) %>%
  ggplot(aes(x=slices, y = factor(1), fill = lbls)) +
  geom_col() +
  guides(fill = guide_legend(reverse = TRUE)) +
  coord_polar()

enter image description here

If we add these lines:

... + 
geom_text(aes(label = slices), position = position_stack(vjust = 0.5)) +
theme_void() # to remove axis lines and background shading

we can get the values labeled inside each slice. We have to use the position argument so that geom_text can know that we want our text labels stacked incrementally around the pie, same as the slices are stacked automatically in geom_col(). vjust says we want the labels halfway into each slice, not at either side edge.

enter image description here

轻拂→两袖风尘 2025-01-20 11:26:19

尝试在ggplot中尝试aes(x="", ...)并可能调整start = ...direction = .. .coor_polar 中。

Try aes(x="", ... in ggplot and maybe adjust start = ... or direction = ... in coor_polar.

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