Два перекрывающихся скрипичных сюжета, пытающихся добавить цвета и леген - PullRequest
0 голосов
/ 23 апреля 2020

Здравствуйте, я пытаюсь нанести два скрипача один на другой. Я нашел здесь полезные посты и подготовил следующий код:

nums <- c(rep(1,12),rep(2,32),rep(3,19))
type <- rep('violin_green',63)

df1 <- data.frame(nums=nums,type=type)

nums <- c(rep(1,3),rep(2,15),rep(3,45))
type <- rep('violin_blue',63)

df2 <- data.frame(nums=nums,type=type)

p <- ggplot(df1, aes(factor(type), nums))
p <- p + geom_violin(aes(colour = df1$type, fill='red'), alpha = 0.3, trim=F)
q <- p + geom_violin(aes(y = df2$nums, colour = df2$type, fill='deepskyblue1'), alpha = 0.3, trim=F)
q + theme_bw() + ggtitle('Test title') + xlab('Test x')

, который дает:

enter image description here

Но я бы хотел изменить цвета для зеленого и синего, как во фреймах данных: violin_green и violin_blue вместо этих цветов по умолчанию. Изменение fill = ничего не делает, кроме обмена цветами границ скрипки.

Я пробовал с scale_color_manual и scale_fill_manual:

p <- ggplot(df1, aes(factor(type), nums))
p <- p + geom_violin(aes(df1$type, colour='violin_green_color', fill='violin_green_fill'), alpha = 0.3, trim=F) + 
  scale_color_manual(name='',values=c(violin_green_color = 'darkgreen')) +
  scale_fill_manual(name='',values=c(violin_green_fill = 'green'))
p
q <- p + geom_violin(aes(y = df2$nums, colour = 'violin_blue_color', fill='violin_blue_fill'), alpha = 0.3, trim=F) +
  scale_color_manual(name='',values=c(violin_blue_color = 'darkblue')) +
  scale_fill_manual(name='',values=c(violin_blue_fill = 'blue'))
q + theme_bw() + ggtitle('Test title') + xlab('Test x')

Результат:

enter image description here

Но я не могу построить вторую скрипку из-за:

Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.

Я также хотел бы иметь хороший простой легенда справа. Не могли бы вы помочь мне с этим?

1 Ответ

0 голосов
/ 23 апреля 2020

Хорошо, я думаю, что получил это!

p <- ggplot(df1, aes(factor(type), nums))
p <- p + geom_violin(aes(df1$type, colour = 'darkgreen', fill='green'), alpha = 0.3, trim=F)
q <- p + geom_violin(aes(y = df2$nums, colour = 'darkblue', fill='blue'), alpha = 0.3, trim=F)
q <- q + scale_colour_manual(name='',values=c('darkblue','darkgreen')) +
  scale_fill_manual(name='',values=c('blue','green'))
q + theme_bw() + ggtitle('Test title') + xlab('Test x')

enter image description here

...