r - ggplot2 legend only showing partial border with legend.key attribute -


i'm trying create graph using ggplot2, , don't understand why legend.key attribute placing border around part of legend. see minimal working example below. suggestions on fix? example provided here seems work fine (https://github.com/hadley/ggplot2/wiki/legend-attributes#legendkey-rect).

set.seed(12) xy <- data.frame(x=c(rep(letters[1], times=25), rep(letters[2], times=25)),                   y=rep(letters[1:25], times=2),                  type = sample(c(-2,-1,0,1,2), 50, replace=t)) xy$type <- factor(xy$type, c(-2, -1, 0, 1, 2))  ggplot(xy, aes(x=x, y=y, fill=type, height=0.95)) +      geom_tile() +     scale_fill_manual(values = c("#323d8d", "#ffffff", "#ffffff", "#ffffff", "#ff0000"),                        na.value="#bebebe") +     scale_x_discrete(expand=c(0,0.51)) +     theme(legend.key = element_rect(colour="black")) +     theme(axis.ticks.x = element_blank()) +     theme(axis.ticks.y = element_blank()) +     theme(panel.grid.major.y=element_blank()) +     theme(panel.grid.major.x=element_blank()) +     theme(panel.grid.minor.y=element_blank()) +     theme(panel.grid.minor.x=element_blank()) +     theme(panel.background = element_rect(fill="#000000")) 

enter image description here

i not sure why, appears has rendering image screen in rstudio (i assume working in rstudio).

all have do, use ggsave("plot.png") , lines draw expected. if save pdf, legend have desired lines.

and, @jeremycg stated, if want lines show on screen, should make them little thicker legend.key = element_rect(colour="black", size=1)) (size=1 enough on screen).

finally: don't need type theme command every time, can use "," seperate them, like:

 theme(legend.key = element_rect(colour="black"),        axis.ticks.x = element_blank(),        axis.ticks.y = element_blank()) 

i didn't test it, think faster way.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -