更改圆环图中标签的位置
Changing the position of the label in a donut chart
我想将圆环图中标签的位置移动到图表的右侧,但我做不到。我正在使用 ggplot 和 ggrepel 来制作图表。
library(ggplot2)
library(ggrepel)
expenditurePie = data.frame(
value = c(98,2),
area = c("A","B"),
label = c("","This is a label"))
ggplot(expenditurePie, aes(y=value, fill = area, label = label)) +
geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
geom_text_repel(size = 5, x= 4, point.padding = unit(1.8, "lines"), direction = "x") +
xlim(0.5, 4.5) +
annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 16, color = "grey") +
scale_fill_manual(values = c(A = "grey", B = "black")) +
coord_polar(theta = "y", start = 1) +
theme_void() +
theme(legend.position = 'none')
下图是上面代码的结果:
但下图显示了我所需要的:
我该怎么做?谢谢
这对你有用吗?利用 nudge_x=5
library(ggplot2)
library(ggrepel)
expenditurePie = data.frame(
value = c(98,2),
area = c("A","B"),
label = c("","This is a label"))
ggplot(expenditurePie, aes(y=value, fill = area, label = label)) +
geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
geom_text_repel(size = 5, x= 4, point.padding = unit(1.8, "lines"),
direction = "x", nudge_x = 5) +
xlim(0.5, 4.5) +
annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 16, color =
"grey") +
scale_fill_manual(values = c(A = "grey", B = "black")) +
coord_polar(theta = "y", start = 1) +
theme_void() +
theme(legend.position = 'none')
输出:
也许这是您的选择。我的方法不是依赖 ggrepel
的魔力,而是手动设置标签(仍然使用 ggrepel
但力设置为 0)并使用 geom_segement
绘制线段。试试这个:
library(ggplot2)
library(ggrepel)
expenditurePie = data.frame(
value = c(98,2),
area = c("A","B"),
label = c("","This is a label"))
ggplot(expenditurePie, aes(y = value, fill = area, label = label)) +
geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
geom_text_repel(size = 5, x = 7, point.padding = unit(1.8, "lines"), direction = "x", force = 0, seed = 42) +
geom_segment(aes(x = 4.6, xend = 5.5, y = 1, yend = 1)) +
xlim(0.5, 5.5) +
annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 20, color = "grey") +
scale_fill_manual(values = c(A = "grey", B = "black")) +
coord_polar(theta = "y", start = 1) +
theme_void() +
theme(legend.position = 'none')
由 reprex package (v0.3.0)
于 2020-05-25 创建
我想将圆环图中标签的位置移动到图表的右侧,但我做不到。我正在使用 ggplot 和 ggrepel 来制作图表。
library(ggplot2)
library(ggrepel)
expenditurePie = data.frame(
value = c(98,2),
area = c("A","B"),
label = c("","This is a label"))
ggplot(expenditurePie, aes(y=value, fill = area, label = label)) +
geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
geom_text_repel(size = 5, x= 4, point.padding = unit(1.8, "lines"), direction = "x") +
xlim(0.5, 4.5) +
annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 16, color = "grey") +
scale_fill_manual(values = c(A = "grey", B = "black")) +
coord_polar(theta = "y", start = 1) +
theme_void() +
theme(legend.position = 'none')
下图是上面代码的结果:
但下图显示了我所需要的:
我该怎么做?谢谢
这对你有用吗?利用 nudge_x=5
library(ggplot2)
library(ggrepel)
expenditurePie = data.frame(
value = c(98,2),
area = c("A","B"),
label = c("","This is a label"))
ggplot(expenditurePie, aes(y=value, fill = area, label = label)) +
geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
geom_text_repel(size = 5, x= 4, point.padding = unit(1.8, "lines"),
direction = "x", nudge_x = 5) +
xlim(0.5, 4.5) +
annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 16, color =
"grey") +
scale_fill_manual(values = c(A = "grey", B = "black")) +
coord_polar(theta = "y", start = 1) +
theme_void() +
theme(legend.position = 'none')
输出:
也许这是您的选择。我的方法不是依赖 ggrepel
的魔力,而是手动设置标签(仍然使用 ggrepel
但力设置为 0)并使用 geom_segement
绘制线段。试试这个:
library(ggplot2)
library(ggrepel)
expenditurePie = data.frame(
value = c(98,2),
area = c("A","B"),
label = c("","This is a label"))
ggplot(expenditurePie, aes(y = value, fill = area, label = label)) +
geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
geom_text_repel(size = 5, x = 7, point.padding = unit(1.8, "lines"), direction = "x", force = 0, seed = 42) +
geom_segment(aes(x = 4.6, xend = 5.5, y = 1, yend = 1)) +
xlim(0.5, 5.5) +
annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 20, color = "grey") +
scale_fill_manual(values = c(A = "grey", B = "black")) +
coord_polar(theta = "y", start = 1) +
theme_void() +
theme(legend.position = 'none')
由 reprex package (v0.3.0)
于 2020-05-25 创建