相关疑难解决方法(0)

当ggplot2中有多个图有一些图例而其他图没有图例时,可以对齐多个图

我使用此处指示的方法来对齐共享相同横坐标的图形.

但是当我的一些图表有一个传奇而其他图表没有传说时,我无法使它工作.

这是一个例子:

library(ggplot2)
library(reshape2)
library(gridExtra)

x = seq(0, 10, length.out = 200)
y1 = sin(x)
y2 = cos(x)
y3 = sin(x) * cos(x)

df1 <- data.frame(x, y1, y2)
df1 <- melt(df1, id.vars = "x")

g1 <- ggplot(df1, aes(x, value, color = variable)) + geom_line()
print(g1)

df2 <- data.frame(x, y3)
g2 <- ggplot(df2, aes(x, y3)) + geom_line()
print(g2)

gA <- ggplotGrob(g1)
gB <- ggplotGrob(g2)
maxWidth <- grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
gA$widths[2:3] <- maxWidth
gB$widths[2:3] <- maxWidth
g <- arrangeGrob(gA, gB, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

24
推荐指数
5
解决办法
5477
查看次数

ggmap/ggplot2中多个图例的对齐

我试图用两个表示形状和颜色的图例来制作地图(下面的例子中为"Type"和"Org"),并且有图例插图.我可以放置传说,但我希望它们左对齐,以便它们的左边缘对齐.除了相互集中之外,我无法做出任何其他事情:

require(ggplot2)
require(ggmap)
require(grid)
require(mapproj)

data <- data.frame(Org=rep(c("ABCDEFG","HIJKLMNOP","QRSTUVWX"),4)
                   , Type=rep(c("Y","Z"),6), Lat=runif(12,48,54.5)
                   , Long=runif(12,-133.5,-122.5))

osmMap <- get_map(location=c(-134,47.5,-122,55), source = 'osm')

points <- geom_jitter(data=data, aes(Long, Lat, shape=Type
                                     , colour=Org))

legend <- theme(legend.justification=c(0,0), legend.position=c(0,0)
                , legend.margin=unit(0,"lines"), legend.box="vertical"
                , legend.key.size=unit(1,"lines"), legend.text.align=0
                , legend.title.align=0)

ggmap(osmMap) + points + legend
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r ggplot2 ggmap r-grid

17
推荐指数
1
解决办法
4288
查看次数

如何用长标签维护ggplot的大小

我有一个情节,它是每种事件类型的简单条形图.我需要情节的标签在情节之下,因为一些事件有很长的名字并且横向压扁了情节.我试图在标题下面移动标签,但是当有很多事件类型时,它现在会被压扁.有没有办法有一个静态的地块大小(即条形图),以便长传说不会压缩情节?

我的代码:

ggplot(counts_df, aes(x = Var2, y = value, fill - Var1)+
    geom_bar(stat = "identity") +
    theme(legend.position = "bottom") +
    theme(legen.direction = "vertical") +
    theme(axis.text.x = element_text(angle = -90)
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

我认为这是因为图像尺寸必须是静态的,因此曲线会因轴而牺牲.当我在情节下面放置一个传奇时,同样的事情发生了.

r ggplot2

16
推荐指数
3
解决办法
9378
查看次数

在两个图之间保持ggplot2中所有绘图组件的大小相同

我想要两个独立的情节.我在一个投影仪演示的不同帧中使用它们,我将向另一个添加一行(最终,不在下面的示例中).因此,我不希望演示文稿从一张幻灯片"跳过"("跳转"?)到下一张幻灯片.我希望它看起来像是自然添加的线.我认为以下代码显示了问题.这是微妙的,但不是如何第二个图的绘图区域略大于第一个图.这是因为y轴标签.

library(ggplot2)

dfr1 <- data.frame(
  time = 1:10,
  value = runif(10)  
)

dfr2 <- data.frame(
  time = 1:10,
  value = runif(10, 1000, 1001)  
)

p1 <- ggplot(dfr1, aes(time, value)) + geom_line() + scale_y_continuous(breaks = NULL) + scale_x_continuous(breaks = NULL) + ylab(expression(hat(z)==hat(gamma)[1]*time+hat(gamma)[4]*time^2))
print(p1)
dev.new()
p2 <- ggplot(dfr2, aes(time, value)) + geom_line() + scale_y_continuous(breaks = NULL) + scale_x_continuous(breaks = NULL) + ylab(".")
print(p2)
Run Code Online (Sandbox Code Playgroud)

我宁愿没有一个hackish解决方案,如手动设置轴标签的大小或在x轴上添加空格(参见下面的一个参考),因为我将在几个设置中使用此技术,标签可以在任何设置更改时间(我喜欢再现性,所以想要一个灵活的解决方案).

我搜索了很多,发现了以下内容:

指定ggplot2面板宽度

如何在ggplot(带图例)中制作一致宽度的图?

https://groups.google.com/forum/#!topic/ggplot2/2MNoYtX8EEY

如何在不改变绘图宽度的情况下在R中使用ggplot2添加可变大小的y轴标签?

它们对我不起作用,主要是因为我需要单独的图,因此不需要像上面的一些解决方案那样在一个组合图上对齐它们.

r ggplot2

13
推荐指数
1
解决办法
2519
查看次数

使用facetted ggplot 2.0.0和gridExtra排列公共绘图宽度

由于我已经更新到ggplot2 2.0.0,我无法使用gridExtra正确排列图表.问题是分面图表会被压缩,而其他图表会扩展.宽度基本搞砸了.我想安排它们类似于这些单面图的方式:左对齐两个图形边(ggplot)

我把一个可重现的代码

library(grid) # for unit.pmax()
library(gridExtra)

plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + 
  facet_grid(. ~ Species) + 
  stat_smooth(method = "lm")

plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
  geom_point(size=2.5)

g.iris <- ggplotGrob(plot.iris) # convert to gtable
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable

iris.widths <- g.iris$widths # extract the first three widths, 
mpg.widths <- g.mpg$widths # same for mpg plot
max.widths <- unit.pmax(iris.widths, mpg.widths)

g.iris$widths <- max.widths # assign max. widths …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gridextra gtable

6
推荐指数
2
解决办法
3618
查看次数

使用gtable创建ggplot图(具有相同宽度的凹槽)以创建2x2布局

我试图使用grobs和gtable将4(ggplot2)图排列成2x2网格.我不知道如何设置宽度,也不知道非1xn或nx1排列.

使用此代码:

data(iris)
a <- ggplot(iris, aes(x=Species, y=Petal.Width)) + geom_boxplot(color="black") + ylab(expression(Foo~Bar~(g~cm^{-3})))
b <- ggplot(iris, aes(x=Species, y=Petal.Length*100)) + geom_boxplot(color="black") + ylab("foobar (mm)")
c <- ggplot(iris, aes(x=Species, y=Sepal.Width)) + geom_boxplot(color="black") + ylab("foobar (%)")
d <- ggplot(iris, aes(x=Species, y=log10(Sepal.Length))) + geom_boxplot(color="black") + ylab("foobar (cm)")

plots <- list(a,b,c,d)
grobs = lapply(plots, ggplotGrob)
g = do.call(rbind, c(grobs, size="first"))

g$widths = do.call(unit.pmax, lapply(grobs, "[[", "widths"))
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)

我可以创建以下1x4安排. 1乘4的情节安排

如果我将grid.arrange用于两列,则在4个图上,这些图的宽度不同.

4乘4的绘图排列 - 不等宽度

如何将图表绑定到4 x 4排列的gtable中?

# I thought maybe I could cbind, then rbind, but this does not …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 grob gtable

4
推荐指数
1
解决办法
6527
查看次数

标签 统计

ggplot2 ×6

r ×6

gtable ×2

ggmap ×1

gridextra ×1

grob ×1

r-grid ×1