2017-03-22 81 views
3

如何更改下方标题的关键字,使其处于水平位置而不改变图形的垂直线。如何在ggplot中更改图例的“Key”的方向?

set.seed(000) 
m <- matrix(rnorm(100,0,1),100,1) 
dt <- data.frame(m) 
names(dt) <- c("X") 

library(ggplot2) 

g2 <- ggplot(dt, aes(x=X)) 
g2 <- g2+geom_histogram(aes(y=..density..),  # Histogram with density instead of count on y-axis 
         binwidth=.5, 
         colour="black", fill="white",breaks=seq(-2, 2, by = 0.1)) 
g2 <- g2+geom_density(alpha=.3, fill="#cccccc") # Overlay with transparent density plot 
g2 <- g2+ geom_vline(aes(xintercept=0, linetype="Valor Verdadeiro"),show.legend =TRUE) 
g2 <- g2+ geom_vline(aes(xintercept=mean(dt$X, na.rm=T), linetype="Valor Estimado"),show.legend =TRUE) 
g2 <- g2+ scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot 
g2 <- g2+ xlab(expression(paste(gamma[1])))+ylab("Densidade") 
g2 <- g2+ theme(legend.key.height = unit(2, "cm") , 
       legend.position = c(0.95, 0.95), 
       legend.justification = c("right", "top"), 
       legend.box.just = "right", 
       legend.margin = margin(6, 6, 6, 6), 
       legend.title=element_blank(), 
       legend.direction = "vertical", 
       legend.background = element_rect(fill="gray", size=.5, linetype="dotted")) 
g2 <- g2+ guides(linetype = guide_legend(override.aes = list(size = 1))) 
g2 

注:我想从在dotdashsolid格式字幕内旋转的线。

enter image description here

+0

你可能要检查这个解决方案: http://stackoverflow.com/questions/35703983/how-to-change-angle-of-line-in-customized-legend- in-ggplot2 –

+0

@JeroenBoeye它不起作用,我试过了! – fsbmat

回答

2

你可能不得不求助于与ggplot GROB工作,并使用grid编辑功能。

# Your data and plot 
set.seed(000) 
m <- matrix(rnorm(100,0,1),100,1) 
dt <- data.frame(m) 
names(dt) <- c("X") 

library(ggplot2) 

g2 <- ggplot(dt, aes(x=X)) 
g2 <- g2+geom_histogram(aes(y=..density..),  # Histogram with density instead of count on y-axis 
         binwidth=.5, 
         colour="black", fill="white",breaks=seq(-2, 2, by = 0.1)) 
g2 <- g2+geom_density(alpha=.3, fill="#cccccc") # Overlay with transparent density plot 
g2 <- g2+ geom_vline(aes(xintercept=0, linetype="Valor Verdadeiro"),show.legend =TRUE) 
g2 <- g2+ geom_vline(aes(xintercept=mean(dt$X, na.rm=T), linetype="Valor Estimado"),show.legend =TRUE) 
g2 <- g2+ scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot 
g2 <- g2+ xlab(expression(paste(gamma[1])))+ylab("Densidade") 
g2 <- g2+ theme(legend.key.height = unit(2, "cm") , 
       legend.position = c(0.95, 0.95), 
       legend.justification = c("right", "top"), 
       legend.box.just = "right", 
       legend.margin = margin(6, 6, 6, 6), 
       legend.title=element_blank(), 
       legend.direction = "vertical", 
       legend.background = element_rect(fill="gray", size=.5, linetype="dotted")) 
g2 <- g2+ guides(linetype = guide_legend(override.aes = list(size = 1))) 


# Adjust key height and width 
g2 = g2 + theme(
    legend.key.height = unit(.6, "cm"), 
    legend.key.width = unit(1, "cm")) 

# Get the ggplot Grob 
    gt = ggplotGrob(g2) 

# grid.ls(grid.force(gt)) # To get a list of editable grobs 

# Edit the relevant keys 
library(grid) 
gt <- editGrob(grid.force(gt), gPath("key-[3,4]-1-[1,2]"), 
     grep = TRUE, global = TRUE, 
     x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
     x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

# Draw it 
grid.newpage() 
grid.draw(gt) 

enter image description here

+0

谢谢!一个问题,如果我更改'legend.direction =“horizo​​ntal”,“我可以在哪里更改代码以保持密钥水平? – fsbmat

+1

'grid.ls(grid.force(gt)'给出可编辑grobs的列表。使用水平图例,相关的grobs开头为:key-1-3-1; key-1-3-2; key-1 -7-1; key-1-7-2。因此,editGrob命令中的gPath将是:'gPath(“key-1- [3,7] - [1,2]”)' –

+0

非常感谢太多了!你帮了我很多! – fsbmat