How to maintain a constant text scale and x-axis scale on multiple ungrouped charts

I currently have a script that generates several drawings of the same type at startup (building the effect of various treatments on the same categories in my dataset), which all shy away from histograms with the same values ​​in the x axis). An example is:

d <- data.frame(x = runif(1000), y = runif(1000)^2, very_long_label_name = runif(1000)^3)
d <- round(d, 1)
d <- melt(d)
qplot(data = d[d$variable != "very_long_label_name",], factor(value), position = "dodge", 
      geom = "histogram", fill = variable)
ggsave("test1.png", height = 3.5, width = 4.5)
qplot(data = d[d$variable != "y",], factor(value), position = "dodge",
      geom = "histogram", fill = variable)
ggsave("test2.png", height = 3.5, width = 4.5)

As my numbers shy away from the histograms, I have a legend on the side for the color of the bars. But since each digit compares different treatments, the labels on the legends have different lengths, so the numbers eventually appear with different proportions and sizes of text. Is there a way to control the size of the text and the width of the x axis on multiple shapes?

ggplot R?, coord_fixed(), , ggsave(). ggplot ( )? - , , , , . theme_set() , , , , ggsave().

, , , - , , ggsave("test.png", width = 3). ggplot2?

+4
2

( , ) , ( ), . , . , . :

library(gridExtra) 

p1 = qplot(data = d[d$variable != "very_long_label_name",], factor(value), 
           position = "dodge", geom = "histogram", fill = variable)

p2 = qplot(data = d[d$variable != "y",], factor(value), position = "dodge",
      geom = "histogram", fill = variable)

, , , :

# Function to extract legend as a separate grob
# Source: http://stackoverflow.com/a/12539820/496488
get_leg = function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  legend
}

# Function to left justify the legends so they line up
# Source: http://stackoverflow.com/a/25456672/496488
justify <- function(x, hjust="center", vjust="center", draw=TRUE){
  w <- sum(x$widths)
  h <- sum(x$heights)
  xj <- switch(hjust,
               center = 0.5,
               left = 0.5*w,
               right=unit(1,"npc") - 0.5*w)
  yj <- switch(vjust,
               center = 0.5,
               bottom = 0.5*h,
               top=unit(1,"npc") - 0.5*h)
  x$vp <- viewport(x=xj, y=yj)
  if(draw) grid.draw(x)
  return(x)
}

:

# Extract each legend
leg1 = get_leg(p1)
leg2 = get_leg(p2)

# Allocate proportions of layout width to plot and legend
w = c(0.6,0.4)

# Lay out plot and legend separately
png("test1.png", height = 3, width = 6, units="in", res=100)
grid.arrange(p1 + theme(legend.position="none"), 
             justify(leg1,"left","center"),
             widths=w, ncol=2)
dev.off()

png("test2.png", height = 3, width = 6, units="in", res=100)
grid.arrange(p2 + theme(legend.position="none"), 
             justify(leg2,"left","center"),
             widths=w, ncol=2)
dev.off()

enter image description here

enter image description here

, : y , , - , y- ( ), y . , . :

# New plot with a different data set
p3 = ggplot(iris, aes(Sepal.Length, Sepal.Width*1e6, colour=Species)) +
        geom_point()

leg3 = get_leg(p3)

# Justify widths
# Source: http://stackoverflow.com/a/13295880/496488
gA <- ggplotGrob(p1 + theme(legend.position="none"))
gB <- ggplotGrob(p3 + theme(legend.position="none"))
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)

png("test1a.png", height = 3, width = 6, units="in", res=100)
grid.arrange(gA, justify(leg1,"left","center"),
             widths=w, ncol=2)
dev.off()

png("test3.png", height = 3, width = 6, units="in", res=100)
grid.arrange(gB, justify(leg3,"left","center"),
widths=w, ncol=2)
dev.off()

p1, p1 p3, :

enter image description here

enter image description here

enter image description here

+2

, :

qplot(data = d[d$variable != "very_long_label_name",], factor(value), 
      position = "dodge", geom = "histogram", fill = variable) +
  theme(legend.position = 'bottom')

qplot(data = d[d$variable != "y",], factor(value),
      position = "dodge", geom = "histogram", fill = variable)  +
  theme(legend.position = 'bottom')

enter image description here

enter image description here

0

Source: https://habr.com/ru/post/1620619/


All Articles