如何使ggplot中的文本更具可读性

我正在尝试模拟类似图片的情节,如何使我的情节的白色文本更具可读性,如下面的图片。

我模拟的图​​片:

如何使ggplot中的文本更具可读性

我的情节:

如何使ggplot中的文本更具可读性

我的代码:

# remotes::install_github("GuangchuangYu/nCov2019")
# get COVID-19 data
require(nCov2019)
y = load_nCov2019(lang = 'zh')
d = y['global']

# filter data
require(dplyr)

dd <- filter(d,time == time(y) & country != '中国') %>% 
  arrange(desc(cum_confirm))

dd = dd[1:40,]
dd$country = factor(dd$country,levels = dd$country)

dd$angle = 1:40*360/40

# plot data
require(ggplot2)

 ggplot(dd,aes(country,cum_confirm,fill = cum_confirm)) +
  geom_col(width = 1,color = 'grey90') +
  geom_col(aes(y = I(2)),width = 1,fill = 'white') +
  scale_y_log10()+
  scale_fill_gradientn(colours = c('darkgreen','green','orange','firebrick','red'),trans = 'log') +
  geom_text(aes(label = paste(country,sep = '\n'),y = cum_confirm*0.8,angle = angle),data = function(d) d[d$cum_confirm > 100,],color = 'white',fontface = 'bold',vjust = 1)+
  geom_text(aes(label = paste0(cum_confirm," ",country),y = cum_confirm*5,angle = angle + 90),data = function(d) d[d$cum_confirm < 100,vjust = 0) +
  coord_polar(direction = -1) +
  theme_void()+
  theme(legend.position = "none")



dingdingzhengwei 回答:如何使ggplot中的文本更具可读性

这经常发生在我身上。我通常使用quartz(),然后以较大的尺寸ggsave我的输出,尺寸为12x12、15x15等。很好奇是否有帮助。

,

根据所需的输出格式(pdf,png等)使用设备。由于特殊字符,我使用cairo_pdf设备,但根据您的设置,仅使用pdf设备也可以正常工作。根据标签的长度,您需要使用输出设置来播放 lot

gg_ncov <- ggplot(dd,aes(country,cum_confirm,fill = cum_confirm)) +
  geom_col(width = 1,color = 'grey90') +
  geom_col(aes(y = I(2)),width = 1,fill = 'white') +
  scale_y_log10()+
  scale_fill_gradientn(colours = c('darkgreen','green','orange','firebrick','red'),trans = 'log') +
  geom_text(aes(label = paste(country,sep = '\n'),y = cum_confirm*0.8,angle = angle),data = function(d) d[d$cum_confirm > 100,],color = 'white',fontface = 'bold',vjust = 1)+
  geom_text(aes(label = paste0(cum_confirm," ",country),y = cum_confirm*5,angle = angle + 90),data = function(d) d[d$cum_confirm < 100,vjust = 0) +
  coord_polar(direction = -1) +
  theme_void()+
  theme(legend.position = "none")

cairo_pdf("./graph_ncov.pdf",width = 21,height = 21)
gg_ncov
dev.off()
本文链接:https://www.f2er.com/2647256.html

大家都在问