How to change the "Key" orientation of a Caption in ggplot?

Asked

Viewed 1,312 times

4

how can I change the "key" of the caption below, so that it stays in the horizontal position, without changing the vertical lines of the chart. Also, how to leave the caption background with the same color as the first layer (or "layer") of the chart?

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


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 

inserir a descrição da imagem aqui

2 answers

2

To horizontal the caption, simply change the part legend.direction = "vertical" for legend.direction = "horizontal".

And to change the background of the legend to the same of the chart, just change the legend.background for fill = transparent.

That is to say:

legend.background = element_rect(fill="transparent", size=.5, linetype="dotted")

Your new code would be:

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 = "left",
                 legend.margin = margin(6, 6, 6, 6),
                 legend.title=element_blank(),
                 legend.direction = "horizontal", 
                 legend.background = element_rect(fill="transparent", size=.5, linetype="dotted"))
g2 <- g2+ guides(linetype = guide_legend(override.aes = list(size = 1)))
g2 
  • Actually, I want to rotate is not the whole caption, but the "Key" of it. On this page (http://moderngraphics11.pbworks.com/f/ggplot2-Book09hWickham.pdf) book you can see what is "key".

  • I want to rotate the line inside the legend that is in the format dotdash and solid.

  • @fsbmat understood, the more work, I will only have time to research later

  • OK, thank you very much!

1


I managed to resolve the issue in the stack in English, follows the solution:

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)

inserir a descrição da imagem aqui

  • fsbmat, you can accept your own answer, as it solved the problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.