Текст слишком плотный из полукругов в ggplot2 - PullRequest
1 голос
/ 17 июня 2020

У меня есть небольшой набор данных, как показано ниже:

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)

Out:

enter image description here

Как видите, геометрический текст Японии и Германии слишком плотный. Поэтому мне интересно, есть ли способ отрегулировать положение текста без изменения размера текста.

Я пробовал изменять значения vjust, hjust, но, похоже, это не работает.

Спасибо .

ОБНОВЛЕНИЕ:

Добавив: geom_text_repel(aes(label=value), vjust=0, hjust = 0, size = 2.5)

Часть вывода моих реальных данных:

enter image description here

Добавляя geom_text(aes(label=value), size = 2.5, nudge_y = 0.5)

Часть вывода моих реальных данных:

enter image description here

1 Ответ

1 голос
/ 17 июня 2020

Пробовал разными способами:

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)

enter image description here

Another way is to use nudge_y (I am not sure about the behavour of this method though):

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
  )

Update:

If you adjust the overall size of the graph, you may get something like this:

df 

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...