简体   繁体   English

在ggplot2中,使用两种图形类型时如何缩放图例?

[英]In ggplot2 how can I scale the legend when using two graph types?

I'm using ggplot2 with both + geom_line() + geom_point(). 我正在将ggplot2与+ geom_line()+ geom_point()一起使用。 I have the colors/shapes worked out, but I can't scale the legend appropriately. 我已经解决了颜色/形状,但是无法适当缩放图例。 If I do nothing it's tiny, and if I enlarge it, the color blocks the shape. 如果我什么都不做,那么它很小,如果我放大它,颜色就会挡住形状。

For example: 例如: 在此处输入图片说明

You can see that the shapes and colors are both in the legend, but the shapes are being drawn over by the colors. 您可以看到形状和颜色都在图例中,但是形状正在被颜色绘制。 I would like to have shapes of the appropriate color drawn in the legend, but can't figure out how to do it. 我想在图例中绘制适当颜色的形状,但不知道该怎么做。

My plot is being drown as follows: 我的情节被淹死如下:

ggplot(data=melted, aes(x=gene, y=value, colour=variable, shape=variable, group = variable, stroke=3, reorder(gene, value))) 
+ theme_solarized() 
+ scale_colour_solarized("blue") 
+ geom_line() 
+ geom_point() 
+ theme(axis.text.x = element_text(angle = 90, hjust = 1), plot.title = element_text(size=16, face="bold"), legend.title=element_blank(), legend.text=element_text(size=20)) 
+ ggtitle('Signiture Profiles') 
+ labs(x="Gene", y=expression(paste("Expression"), title="Expression"))  
+ scale_colour_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c("#ff420e","#89da59","#89da59","#89da59","#376467","#376467","#376467","#00293c","#00293c","#00293c")) 
+ scale_shape_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c(0,1,2,3,1,2,3,1,2,3)) 
+ guides(colour = guide_legend(override.aes = list(size=12)))

Here is some example data as requested: Example Data 以下是一些请求的示例数据示例数据

Thanks in advance for any help you can provide. 在此先感谢您提供的任何帮助。

You could perhaps rethink how you are differentiating your variables. 您也许可以重新考虑如何区分变量。 You could do something like the following. 您可以执行以下操作。 Note the changes in the first line, where I have separated the component parts of variable rather than setting colours and shapes via your scale statements. 请注意第一行的更改,在该行中,我已分隔variable的组成部分,而不是通过您的scale语句设置颜色和形状。 (I haven't got your theme, so I left that out). (我还没有您的主题,所以我省略了)。

ggplot(data=melted, aes(x=gene, 
                        y=value, 
                        colour=gsub("_.*","",variable), 
                        shape=gsub(".*_","",variable), 
                        group = variable, 
                        stroke=3, 
                        reorder(gene, value))) +
  geom_line() +
  geom_point() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1), 
        plot.title = element_text(size=16, face="bold"), 
        legend.title=element_blank(), 
        legend.text=element_text(size=20)) +
  ggtitle('Signiture Profiles') +
  labs(x="Gene", y=expression(paste("Expression"), title="Expression")) + 
  guides(shape = guide_legend(override.aes = list(size=5)),
         colour = guide_legend(override.aes = list(size=5)))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM