反转条形图颜色的顺序,同时保持图例顺序和颜色
我正在 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
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")
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我们添加这些行:
我们可以获得每个切片内标记的值。我们必须使用
position
参数,以便geom_text
知道我们希望文本标签在饼图周围逐渐堆叠,就像切片自动堆叠在geom_col 中一样()
。vjust
表示我们希望标签位于每个切片的中间,而不是两侧边缘。If we add these lines:
we can get the values labeled inside each slice. We have to use the
position
argument so thatgeom_text
can know that we want our text labels stacked incrementally around the pie, same as the slices are stacked automatically ingeom_col()
.vjust
says we want the labels halfway into each slice, not at either side edge.尝试在
ggplot
中尝试aes(x="", ...
)并可能调整start = ...
或direction = .. .
在coor_polar
中。Try
aes(x="", ...
inggplot
and maybe adjuststart = ...
ordirection = ...
incoor_polar
.