我正在使用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)

我一直试图从这里扩展我的场景以利用facets(特别是facet_grid()).
我已经看过这个例子,但是我似乎无法让它为我geom_bar()和geom_point()组合工作.我试图从仅仅改变例子中使用的代码facet_wrap来facet_grid这也似乎让第一层不显示.
对于网格和凹凸,我是一个非常新手,所以如果有人可以提供一些指导,如何使P1显示左侧y轴,P2显示在右侧y轴上,这将是很好的.
数据
library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)
grid.newpage()
dt.diamonds <- as.data.table(diamonds)
d1 <- dt.diamonds[,list(revenue = sum(price),
stones = length(price)),
by=c("clarity","cut")]
setkey(d1, clarity,cut)
Run Code Online (Sandbox Code Playgroud)
p1和p2
p1 <- ggplot(d1, aes(x=clarity,y=revenue, fill=cut)) +
geom_bar(stat="identity") +
labs(x="clarity", y="revenue") +
facet_grid(. ~ cut) +
scale_y_continuous(labels=dollar, expand=c(0,0)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.text.y = element_text(colour="#4B92DB"),
legend.position="bottom")
p2 <- ggplot(d1, aes(x=clarity, y=stones, colour="red")) +
geom_point(size=6) +
labs(x="", y="number …Run Code Online (Sandbox Code Playgroud) grid.arrange在gridExtra包中使用相对简单的方法在矩阵中排列多个图,但是ggplot2当一些图比其他图更大时,如何安排图(我正在处理的图)?在基础上,我可以使用layout()如下例子:
nf <- layout(matrix(c(1,1,1,2,3,1,1,1,4,5,6,7,8,9,9), byrow=TRUE, nrow=3))
layout.show(nf)
Run Code Online (Sandbox Code Playgroud)
ggplot情节的等价物是什么?

包含的一些情节
library(ggplot2)
p1 <- qplot(x=wt,y=mpg,geom="point",main="Scatterplot of wt vs. mpg", data=mtcars)
p2 <- qplot(x=wt,y=disp,geom="point",main="Scatterplot of wt vs disp", data=mtcars)
p3 <- qplot(wt,data=mtcars)
p4 <- qplot(wt,mpg,data=mtcars,geom="boxplot")
p5 <- qplot(wt,data=mtcars)
p6 <- qplot(mpg,data=mtcars)
p7 <- qplot(disp,data=mtcars)
p8 <- qplot(disp, y=..density.., geom="density", data=mtcars)
p9 <- qplot(mpg, y=..density.., geom="density", data=mtcars)
Run Code Online (Sandbox Code Playgroud) 我正在使用非常棒的库ggplot2.我想通过使用如何设置绘图的纵横比coord_fixed.现在,我想将绘图保存为具有指定宽度(例如10 cm)的PDF,并计算所需的高度.我没弄明白如何实现这一目标.这甚至可能吗?
我和这个用户有同样的问题:我想制作一个facet_grid带有离散x轴的图,我想让x轴标签写在每个面下,而不是只在底面的一行面下面.例如:
# Drop some factor levels to make the plot smaller
diamondSub <- subset(diamonds, (cut=="Ideal" | cut=="Premium") &
(color=="E" | color=="I"))
# Note that scales="free_x" has no practical effect here
ggplot(diamondSub, aes(x=clarity, y=price)) +
geom_blank()+
geom_boxplot() +
facet_grid(cut~color, scales="free_x")
Run Code Online (Sandbox Code Playgroud)

但是,我不想使用该帖子的解决方案,而只是使用facet_wrap而不是facet_grid,因为我更喜欢facet_grid用条形文本标记条形文本,列的顶部有一个变量,而另一个变量标注行.
当所有x轴实际上相同时,有没有办法在每个面下获得x轴标签facet_grid?
使用https://gist.github.com/low-decarie/5886616上的代码 可以生成双树枝状瓷砖图:
dual_dendogram_tile_plot(as.matrix(USArrests),main ="USA Arrests")

问题:将垂直树形图与瓷砖绘图区域对齐.(和/或改善水平树状图的对齐)
这个问题涉及到:
题
如何组合单独的图(ggplot2),不同的y轴和不同的绘图高度,但保持对齐?
详情
将图表与grid.arrange(方法1)组合在一起时,使用不同的y轴单位时,它们不会对齐.解决这个问题的一种方法是使用gtable(method2),但我无法调整图的相对高度.
例
require(ggplot2)
#Make two plots, with different y axis
x = c(1, 5)
y= c(.1, .4)
data1<-data.frame(x,y)
top<-
ggplot(data1, aes(x=x, y=y))+
geom_line()
x = c(1, 5)
y= c(100000, 400000)
data2<-data.frame(x,y)
bottom<-
ggplot(data2, aes(x=x, y=y))+
geom_line()
# Method 1 - Grid Extra
require(gridExtra)
grid.arrange(top, bottom, heights=c(.6,.3))
Run Code Online (Sandbox Code Playgroud)
方法1导致此图,由于y轴标签的长度不同,该图未对齐:

#Method 2 - gtable
require(gtable)
#Extract Grobs
g1<-ggplotGrob(top)
g2<-ggplotGrob(bottom)
#Bind the tables
g<-gtable:::rbind_gtable(g1, g2, "first")
#Remove a row between the plots
g <- gtable_add_rows(g, unit(-1,"cm"), pos=nrow(g1)) …Run Code Online (Sandbox Code Playgroud) 在具有多个构面变量的图中,ggplot2重复"外部"变量的构面标签,而不是在"内部"变量的所有级别上具有单个跨越构面条带.我有一直在使用,以覆盖与使用单个跨越小面条重复外端面标签一些代码gtable_add_grob从gtable包.
不幸的是,由于facet strip的grob结构发生了变化,此代码不再适用于ggplot2 2.2.0.具体来说,在ggplot2的早期版本中,facet标签的每一行都有自己的一组grob.但是,在版本2.2.0中,看起来每个facet标签的垂直堆栈都是单个grob.这打破了我的代码,我不知道如何解决它.
这是一个具体的例子,取自几个月前我回答的SO问题:
# Data
df = structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("SF", "SS"), class = "factor"), species = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, …Run Code Online (Sandbox Code Playgroud) 我想在一个刻面的情节中添加一个方面标题作为条带ggplot2.我的MWE抛出一个错误.任何帮助将受到高度赞赏.谢谢
library(ggplot2)
library(gtable)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ cyl)
# get gtable object
Plot1 <- ggplot_gtable(ggplot_build(p))
# add label for top strip
Plot1 <- gtable_add_rows(Plot1, Plot1$heights[[3]], 2)
Plot1 <- gtable_add_grob(Plot1,
list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
textGrob("Cyl", gp = gpar(col = gray(1)))),
3, 4, 3, 10, name = paste(runif(2)))
# add margins
Plot1 <- gtable_add_rows(Plot1, unit(1/8, "line"), 2)
# draw it
grid.newpage()
print(grid.draw(Plot1))
Run Code Online (Sandbox Code Playgroud) 我正在将两个不同的图组合成一个网格布局,grid正如@lgautier在rpy2中使用python所建议的那样.顶部的图是密度,底部是条形图:
iris = r('iris')
import pandas
# define layout
lt = grid.layout(2, 1)
vp = grid.viewport(layout = lt)
vp.push()
# first plot
vp_p = grid.viewport(**{'layout.pos.row': 1, 'layout.pos.col':1})
p1 = ggplot2.ggplot(iris) + \
ggplot2.geom_density(aes_string(x="Sepal.Width",
colour="Species")) + \
ggplot2.facet_wrap(Formula("~ Species"))
p1.plot(vp = vp_p)
# second plot
mean_df = pandas.DataFrame({"Species": ["setosa", "virginica", "versicolor"],
"X": [10, 2, 30],
"Y": [5, 3, 4]})
mean_df = pandas.melt(mean_df, id_vars=["Species"])
r_mean_df = get_r_dataframe(mean_df)
p2 = ggplot2.ggplot(r_mean_df) + \
ggplot2.geom_bar(aes_string(x="Species",
y="value",
group="variable",
colour="variable"),
position=ggplot2.position_dodge(),
stat="identity")
vp_p = …Run Code Online (Sandbox Code Playgroud)