如何改变“键"的方向. ggplot中的图例? [英] How to change the orientation of the "Key" of a Legend in ggplot?

查看:124
本文介绍了如何改变“键"的方向. ggplot中的图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改下面的标题的键,使其处于水平位置而又不更改图形的垂直线.

How can I change the key of the caption below so that it is in the horizontal position without changing the vertical lines of the graph.

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格式的标题内旋转行.

Note: I want to rotate the line from within the caption that is in dotdash and solid format.

推荐答案

您可能不得不求助于ggplot grob并使用grid编辑功能.

You might have to resort to working with the ggplot grob and use grid editing functions.

# 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::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)

这篇关于如何改变“键"的方向. ggplot中的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆