我有一个小的数据集,如下:
df <- data.frame(x = rep(c(1, 2), 3),
type = rep(c("A", "B"), 3),
country = rep(c("Japan", "Germany", "Korea"), each = 2),
count = c(0.01, 0.01, 0.2, 0.2, 3, 6))
df$country <- factor(df$country, levels = c("Japan", "Germany", "Korea"))
我用下面的代码绘制了半个圆圈:
ggplot(df, aes(x=x, y=sqrt(count), fill=type)) + geom_col(width =1) +
coord_polar(theta = "x", direction = -1) +
facet_wrap(~country) +
theme_void()+
geom_text(aes(label=count), vjust=0, hjust = 0, size = 5)
输出:
如您所见,日本和德国的geom文本太密集。因此,我想知道是否有一种方法可以在不更改文本大小的情况下调整文本的位置。
我已经尝试通过修改vjust, hjust
的值来尝试,但似乎不起作用。
谢谢。
UPDATE:
通过添加:geom_text_repel(aes(label=value), vjust=0, hjust = 0, size = 2.5)
我的真实数据输出的一部分:
通过添加geom_text(aes(label=value), size = 2.5, nudge_y = 0.5)
我的真实数据输出的一部分:
我尝试了不同的方式:
library(ggrepel)
ggplot(df, aes(x=x, y=sqrt(count), fill=type)) + geom_col(width =1) +
coord_polar(theta = "x", direction = -1) +
facet_wrap(~country) +
theme_void() +
geom_text_repel(aes(label=count), vjust=0, hjust = 0, size = 5)
另一种方法是使用nudge_y
(尽管我不确定这种方法的行为):
ggplot(df, aes(x=x, y=sqrt(count), fill=type)) + geom_col(width =1) +
coord_polar(theta = "x", direction = -1) +
facet_wrap(~country) +
theme_void() +
geom_text(aes(label=count), size = 5,
nudge_y = .5
)
如果您调整图表的整体大小,可能会得到如下所示:
df <- data.frame(x = rep(c(1, 2), 5),
type = rep(c("A", "B"), 5),
country = rep(c("Japan", "Germany", "Korea", "other1", "other2"), each = 2),
count = c(1208.734, 750.39, 1011.09, 444.83, 944.66, 295.68,
698.17, 340.03,
428.74, 430.15))
df$country <- factor(df$country, levels = c("Japan", "Germany", "Korea", "other1", "other2"))
ggplot(df, aes(x=x, y=sqrt(count), fill=type)) + geom_col(width =1) +
coord_polar(theta = "x", direction = -1) +
facet_wrap(~country, nrow =1) +
theme_void()+
geom_text_repel(aes(label=count),
vjust=0,
hjust = 0,
size = 5)