标签: gtable

对于长图例标题,在ggplot2中居中对齐图例标题和图例键

当传奇标题很长时,我很难将传奇中心的标题相对于图例键对齐.一年前有一个问题适用于短标题,但它似乎不适用于长标题.

示例,首先使用简短的图例标题:

library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) +
  scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
                        name = "A") +
  theme(legend.title.align = 0.5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

一切都如预期的那样,图例标题位于图例键的上方.

现在和一个长长的传奇标题相同:

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) +
  scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
                        name = "Long legend heading\nShould be centered") +
  theme(legend.title.align = 0.5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我们可以看到文本中心与自身对齐,但与图例键无关.我已经尝试修改其他主题选项,例如legend.justification = "center",但似乎没有人从图例框中最左侧的位置移动键.

几条评论:

  • 我几天前正在运行ggplot2的开发版本,v2.2.1.9000.

  • 我特别需要一个连续色阶调色板的解决方案.

r ggplot2 gtable r-grid

11
推荐指数
3
解决办法
3484
查看次数

Draw Lines between Facets of facet_grid

Intro

I want to draw lines between a faceted ggplot. The main goal is to connect those measurements with a line which we want to test against. So basically I want to insert some kind of significance bars inside and between the facets of a ggplot boxplot (or any kind of plot for that matter).

Research

ggsignif

I know that there is ggsignif package which does this for all non faceted plots. There are answers which try to circumvent this …

plot r ggplot2 gtable facet-grid

11
推荐指数
2
解决办法
392
查看次数

在组合ggplot和基本图形时,使面板具有相同的边距

我已经生成了一个结合了ggplot和基本图形的图形:

t <- c(1:(24*14)) 
P <- 24
A <- 10 
y <- A*sin(2*pi*t/P)+20 
#*****************************************************************************
par(mfrow = c(2,1))
plot(y,type = "l",xlab = "Time (hours)",ylab = "Amplitude")
aa <- par("mai")
plot.new()

require(gridBase)
vps <- baseViewports()
pushViewport(vps$figure)
pushViewport(plotViewport(margins = aa)) ## I use 'aa' to set the margins 
#*******************************************************************************
require(ggplot2)
acz <- acf(y, plot = FALSE)
acd <- data.frame(Lag = acz$lag, ACF = acz$acf)
p <- ggplot(acd, aes(Lag, ACF)) + geom_area(fill = "grey") +
  geom_hline(yintercept = c(0.05, -0.05), linetype = "dashed") +
  theme_bw() …
Run Code Online (Sandbox Code Playgroud)

plot r ggplot2 gtable

10
推荐指数
1
解决办法
1374
查看次数

如何在组合的ggplots上添加线,从一个图上的点到另一个图上的点?

我需要在ggplot中重现InDesign中生成的图,以实现可重现性。

在此特定示例中,我将两个图合并到一个合成图中({patchwork}为此我使用了程序包)。

然后,我需要将连接一个图上关键点的线与底部图上的对应点重叠。

这两个图是从相同的数据生成的,具有相同的x轴值,但具有不同的y轴值。

我已经在Stack Overflow上看到了这些示例,但是这些示例处理的是跨构面的绘制线,在这里尝试跨单独的图绘制线时,这在这里不起作用:

我尝试了几种方法,到目前为止,我最接近的方法是:

  1. 使用{grid}package 添加带有grobs的行
  2. 使用将第二个图转换为gtable {gtable},并将面板的剪辑设置为off,以便我可以将线向上延伸到该图的面板之外。
  3. 再次将绘图合并为一张图像{patchwork}

问题出在最后一步,因为x轴现在不再像添加线并将剪辑设置为off之前那样对齐(请参见代码示例)。

我也曾尝试用相结合的情节ggarrange{cowplot}{egg}{patchwork}来自最接近的一次。

以下是我尝试创建的最佳最小reprex的尝试,但仍在捕捉我想要实现的细微差别。

library(ggplot2)
library(dplyr)
library(tidyr)
library(patchwork)
library(gtable)
library(grid)

# DATA
x <- 1:20
data <- data.frame(
  quantity = x,
  curve1 = 10 + 50*exp(-0.2 * x),
  curve2 = 5 + 50*exp(-0.5 * x),
  profit = c(seq(10, 100, by = 10),
             seq(120, -240, by = -40))
)

data_long <- data %>% …
Run Code Online (Sandbox Code Playgroud)

plot r ggplot2 gtable

10
推荐指数
1
解决办法
241
查看次数

如何在双y轴ggplot上显示图例

我正在尝试使用ggplot构建双y轴图表.首先,请允许我说,我不是在讨论是否这样做是好的做法的优点.在查看基于时间的数据以确定2个离散变量的趋势时,我发现它们特别有用.在我看来,对此的进一步讨论更适合交叉验证.

Kohske提供了一个如何做到这一点的非常好的例子,到目前为止我已经习惯了很好的效果.然而,我无法在两个y轴上包含一个图例.我在这里这里也看到过类似的问题,但似乎没有人能解决包含传奇的问题.

我有一个可重现的例子,使用ggplot中的钻石数据集.

数据

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=clarity]

setkey(d1, clarity)
Run Code Online (Sandbox Code Playgroud)

图表

p1 <- ggplot(d1, aes(x=clarity,y=revenue, fill="#4B92DB")) +
  geom_bar(stat="identity") +
  labs(x="clarity", y="revenue") +
  scale_fill_identity(name="", guide="legend", labels=c("Revenue")) +
  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 of stones") + expand_limits(y=0) +
  scale_y_continuous(labels=comma, expand=c(0,0)) +
  scale_colour_manual(name …
Run Code Online (Sandbox Code Playgroud)

charts r ggplot2 gtable data.table

9
推荐指数
1
解决办法
4897
查看次数

ggplot2:使用比视口更少的绘图在网格上绘图

library( ggplot2 )
library( gridExtra )
p1 <- qplot( rnorm( 10 ), rnorm( 10 ) )
p2 <- qplot( rnorm( 10 ), rnorm( 10 ) )
p3 <- qplot( rnorm( 10 ), rnorm( 10 ) )
p4 <- qplot( rnorm( 10 ), rnorm( 10 ) )
p5 <- qplot( rnorm( 10 ), rnorm( 10 ) )
grid.arrange( p1, p2, p3, p4, p5, nrow=2 )
Run Code Online (Sandbox Code Playgroud)

我想把底部的两个情节放在中心位置.怎么办呢?我可以使用它,split.screen但我无法弄清楚如何使用它ggplot2.ggplot2有更酷的图形.

提前致谢!

PK

r ggplot2 gridextra gtable

8
推荐指数
2
解决办法
1202
查看次数

grid.layout不喜欢尊重和复合单位

使用unit.pmaxgtable作为宽度/高度的默认比较证明比我希望的更难; 经过几个小时的搔痒,我把它缩小到这种情况:

library(grid)

w <- list(unit(1,"null"), unit(1,"null"))
class(w) <-  c("unit.list", "unit")
h <- unit(1, "in")
gl1 <- grid.layout(1, 2, widths = w, heights = h,
                   respect = TRUE)
grid.newpage()
grid.show.layout(gl1) # fine

w2 <- w
w2[[1]] <- unit.pmax(unit(1,"null"), unit(1,"null"))
gl2 <- grid.layout(1, 2, widths = w2, heights = h,
                   respect = FALSE)
grid.newpage()
grid.show.layout(gl2)# fine

gl3 <- grid.layout(1, 2, widths = w2, heights = h,
                   respect = TRUE)
grid.newpage()
grid.show.layout(gl3)

## Error in grid.Call.graphics(L_setviewport, vp, TRUE) : 
## …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gtable r-grid

8
推荐指数
1
解决办法
3572
查看次数

ggplot2:使用gtable将条带标签移动到facet_grid的面板顶部

我正在创建一个图形facet_grid,用于在y轴上分割一个分类变量.我决定不使用,facet_wrap因为我需要space = 'free'labeller = label_parsed.我的标签很长,右边有一个图例,所以我想将标签从面板右侧移动到面板顶部.

这是一个显示我陷入困境的例子.

library(ggplot2)
library(gtable)

mt <- ggplot(mpg, aes(x = cty, y = model)) + geom_point() +
  facet_grid(manufacturer ~ ., scales = 'free', space = 'free') +
  theme_minimal() +
  theme(panel.margin = unit(0.5, 'lines'), strip.text.y = element_text(angle = 0))
Run Code Online (Sandbox Code Playgroud)

mt.png

现在我想将条带文本从每个面板的右侧移动到每个面板的顶部.我可以存储条带标签的凹槽并将其从图中删除:

grob <- ggplotGrob(mt)
strips.y <- gtable_filter(grob, 'strip-right')
grob2 <- grob[,-5]
Run Code Online (Sandbox Code Playgroud)

但是现在我被困在rbind了 - 然后回到平板上,所以标签就到了面板的顶部.

另一种可能的解决方案是使用facet_wrap,然后重新大小的面板作为另一个问题讨论,但在这种情况下,我必须手动更改标签上的面,因为没有labeller = label_parsedfacet_wrap.

我对这两种方法的建议表示赞赏!

谢谢阅读,

汤姆

r facet ggplot2 gtable

8
推荐指数
1
解决办法
2421
查看次数

当两个图表具有相同的X轴时,在x轴上对齐双线图和条形图.GGPLOT2

我今天已经尝试了很长一段时间,但仍然无法通过X轴将两种不同的图表类型对齐,这些图表是相同的.当两个图表具有相同的X轴时,我只需要条形图顶部的双线图表.

下面的数据以及我到目前为止编写的代码以及一些不同的尝试.

我的数据:

Date <- c("2015-07-22", "2015-07-23", "2015-07-24", "2015-07-25", "2015-07-26", "2015-07-27", "2015-07-28", "2015-07-29", "2015-07-30", "2015-07-31", "2015-08-01", "2015-08-02", "2015-08-03", "2015-08-04", "2015-08-05")

Mean_temp12z <- c(66.6, 64.6, 67.6, 69.6, 72.0, 71.8, 73.0, 72.2, 72.8, 71.8, 69.4, 64.2, 61.2, 62.0, 63.4)

Mean_temp0z <- c(62.8, 65.0, 67.4, 71.6, 72.4, 71.6, 71.0, 71.8, 71.6, 69.6, 69.2, 66.2, 60.6, 63.6, 66.4)  

temp_deltalow <- c( 3.8, -0.4,  0.2, -2.0, -0.4,  0.2,  2.0,  0.4,  1.2,  2.2,  0.2, -2.0,  0.6, -1.6, -3.0)


GFS_low_changes <- data.frame(Date, Mean_temp12z, Mean_temp0z, temp_deltalow)
Run Code Online (Sandbox Code Playgroud)

到目前为止我绘制的是一个双线图,显示的代码如下图所示:

low_line …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gtable

8
推荐指数
2
解决办法
2845
查看次数

删除ggplot多面图中的一些轴标签

我创建像一个阴谋在这里的人ggplot2包和facet_wrap功能,我想抑制一些x轴的文字,使之更加清晰.

例如,如果x轴刻度仅出现在方框D,F,H和J上,那么它将更清晰.

我怎么能这样做?提前致谢!

编辑:可重现的代码

library(ggplot2)
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)
d + facet_wrap(~ color, nrow = 1)
Run Code Online (Sandbox Code Playgroud)

r facet ggplot2 gtable

7
推荐指数
1
解决办法
1616
查看次数

标签 统计

ggplot2 ×10

gtable ×10

r ×10

plot ×3

facet ×2

r-grid ×2

charts ×1

data.table ×1

facet-grid ×1

gridextra ×1