zhiwei li
zhiwei li

Reputation: 1711

How can I make text in ggplot more readable

I am trying simulate a plot like the picture, how can I make my plot's white text more readable like the picture below.

The picture I simulated:

enter image description here

My plot:

enter image description here

My code:

# 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, cum_confirm, 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")



Upvotes: 1

Views: 443

Answers (2)

knytt
knytt

Reputation: 593

Use the device according to desired output format (pdf, png etc.) Due to special characters I use cairo_pdf device, but with your settings, using only pdf device might work fine as well. With the length of labels you have there, you need to play a lot with the output settings...

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, cum_confirm, 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()

Upvotes: 1

user13062030
user13062030

Reputation:

This happens to me often. I usually use quartz() and then ggsave my outputs with very large dimensions 12x12, 15x15, etc. Curious if this helps.

Upvotes: 1

Related Questions