Как настроить заголовок легенды в ggplot2, когда заголовок имеет 2 строки - PullRequest
0 голосов
/ 10 октября 2019

Я пытаюсь изменить положение заголовка легенды на моем скрипке с помощью ggplot2 (ggplot2_3.2.1). Но когда в заголовке две строки, кажется, что ggplot только корректирует короткую строку, а не перемещает весь заголовок. Я не знаю, является ли это просто ошибкой на ggplot или я что-то не так делаю.

Здесь у вас есть фиктивный пример на случай, если вы мне поможете:

# Libraries
library(tidyverse)
library(hrbrthemes)
library(viridis)

# Load dataset from github
data <-read.table("https://raw.githubusercontent.com/zonination/perceptions/master/probly.csv", header=TRUE, sep=",")
data <- data %>% 
  gather(key="text", value="value") %>%
  mutate(text = gsub("\\.", " ",text)) %>%
  mutate(value = round(as.numeric(value),0)) %>%
  filter(text %in% c("Almost Certainly","Very Good Chance","We Believe","Likely","About Even", "Little Chance", "Chances Are Slight", "Almost No Chance"))
data$len<-rep(1:8,each=46)

# Plot changing the short line on the title
ggplot(data, aes(x=text, y=value, fill=len)) +
  geom_violin(width=2.1, size=0.2) +
  scale_fill_gradient2(low = "#126e6b", mid = "#eeead6",high = "#b74d2c",midpoint = mean(unique(data$len))) +
  theme_ipsum() +
  guides(fill= guide_colorbar(ticks=F,title.hjust = -1, title ="looooooooooong line\nshort line"))+
  coord_flip() +
  xlab("") +
  ylab("(%)")

ggplot(data, aes(x=text, y=value, fill=len)) +
  geom_violin(width=2.1, size=0.2) +
  scale_fill_gradient2(low = "#126e6b", mid = "#eeead6",high = "#b74d2c",midpoint = mean(unique(data$len))) +
  theme_ipsum() +
  guides(fill= guide_colorbar(ticks=F,title.hjust = -1, title ="new short line\nloooooooooooong line"))+
  coord_flip() +
  xlab("") +
  ylab("(%)")

when lower line is the shortest

when upper line is the shortest

...