简体   繁体   中英

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

I'm using ggplot2 with both + 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. (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)))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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