相关疑难解决方法(0)

左对齐两个图形边(ggplot)

我正在使用ggplot并且有两个图表,我希望彼此叠加显示.我使用grid.arrangegridExtra来堆叠它们.问题是,无论轴标签如何,我都希望图形的左边缘与右边缘对齐.(问题出现是因为一个图的标签很短而另一个图很长).

问题:
我该怎么做?我没有和grid.arrange结婚,但ggplot2是必须的.

我尝试了什么:
我尝试使用宽度和高度以及ncol和nrow来制作2 x 2网格并将视觉效果放在相对的角落然后玩宽度但我无法在对角处获得视觉效果.

require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() 
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 
grid.arrange(A, B, ncol=1)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r ggplot2 gridextra gtable

104
推荐指数
6
解决办法
3万
查看次数

ggplot2:排序一个情节

我有一个data.frame,从最高到最低排序.例如:

x <- structure(list(variable = structure(c(10L, 6L, 3L, 4L, 2L, 8L, 
9L, 5L, 1L, 7L), .Label = c("a", "b", "c", "d", "e", "f", "g", 
"h", "i", "j"), class = c("ordered", "factor")), value = c(0.990683229813665, 
0.975155279503106, 0.928571428571429, 0.807453416149068, 0.717391304347826, 
0.388198757763975, 0.357142857142857, 0.201863354037267, 0.173913043478261, 
0.0496894409937888)), .Names = c("variable", "value"), row.names = c(10L, 
6L, 3L, 4L, 2L, 8L, 9L, 5L, 1L, 7L), class = "data.frame")

ggplot(x, aes(x=variable,y=value)) + geom_bar(stat="identity") + 
 scale_y_continuous("",label=scales::percent) + coord_flip() 
Run Code Online (Sandbox Code Playgroud)

现在,数据很好并且排序,但是当我绘制时,它按因子排序.这很烦人,我该如何解决?

r ggplot2

55
推荐指数
4
解决办法
9万
查看次数

geom_tile和facet_grid/facet_wrap用于相同高度的图块

使用ggplot,我想用面板表示图形图块,但每个面板使用相同的高度图块.我有这个图:

dataSta <- list(sites=rep(paste("S", 1:31),each=12), month=rep(1:12,31), value=round(runif(31*12, min=0, max=3000)), panel=c(rep("Group 1",16*12),rep("Group 2", 12*12), rep("Group 3", 3*12)))

    library(ggplot2)
    library(grid)
    base_size <- 9

    windows()
    ggplot(data.frame(dataSta), aes(factor(month), sites)) + 
      geom_tile(aes(fill = value), colour = "black")+
      facet_wrap(~panel, scale="free_y", nrow=3)+
      theme_grey(base_size = base_size) +  
      labs(x = "",y = "") + 
      scale_x_discrete(expand = c(0, 0)) +    
      scale_y_discrete(expand = c(0, 0)) +    
      theme(legend.title = element_blank(),
            axis.ticks = element_blank(),     
            axis.text.x = element_text(size = base_size *0.8, hjust = 0),
            panel.margin = unit(0,"lines"),
            strip.text = element_text(colour="red3", size=10, face=2))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是瓷砖的高度在面板之间是不同的.我尝试使用facet_grid:

windows() …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 facet-wrap

12
推荐指数
1
解决办法
3316
查看次数

标签 统计

ggplot2 ×3

r ×3

facet-wrap ×1

gridextra ×1

gtable ×1