- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前有一个脚本,它在运行时生成多个相同类型的图形(绘制不同处理对我的数据集中相同类别的影响),这些图形都是在 x 轴上具有相同值的躲避条形图)。一个例子是:
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)
coord_fixed()
似乎被
ggsave()
忽略了.
How can I make consistent-width plots in ggplot (with legends)?是一个非常相似的问题,但答案似乎都假设我会将这些数字放在一起而不是将它们散布在一张纸上。使用
theme_set()
至少对于字体大小问题似乎很有希望,除了文本的最终大小会受到
ggsave()
中指定的大小的影响。 .
ggsave("test.png", width = 3)
做。 ggplot2 中是否存在这样的功能?
最佳答案
一种(不幸的是不是那么懒惰)的方法是将图例提取为单独的格罗布(图形对象),然后分别布置图和图例。这使您可以更好地控制分配给每个对象的空间量。幸运的是,一些辅助函数使这个过程相对轻松。下面是一个例子:
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()
# 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()
关于r - 如何在多个未分组的图中保持恒定的文本大小和 x 轴比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34341159/
要求: 我们在数据库中有值 Chennai Baroda Bangalore New Delhi São Paulo, Lisboa San Jose 等... 所以我想将这些字符串转换成一个唯一的短
我需要通过 TCP 加密和发送数据(每条消息从几百字节到几百兆字节)以 block 从 Java 到 C++ 程序,并且需要发送的大小提前发送数据,以便收件人知道何时停止读取当前消息并处理它,然后等待
我不太知道如何问这个问题,因此我将其作为示例: 想象在一个应用程序中您有一个Country对象。此对象有两个属性:Name和'Bordering Countries'集合。稍后可能会添加更多属性,但这
我们正在尝试使用 IoT 中心、流分析和表存储来处理仪表板上的事件驱动图标(开/关/事件)。我无法让实际过程在 6 秒内完成。流分析的水印恒定为 5 秒。该网站声称即时处理。 https://azur
我正在尝试获取一个 JTabbedPane,其中所有选项卡(实际选项卡,而不是组件)具有相同的宽度(最宽标签所需的最小宽度或恒定宽度)。 我试图覆盖 BasicTabbedPaneUI.getTabB
你好安卓开发者, 我正在使用 OpenGLES 1.0 在 Eclipse 中为 Android 开发一个简单的游戏。我正在使用 Samsung Galaxy S2 Android(2.3) 作为开发
我正在尝试使用 ColorMatrix 为整个图像设置一个恒定的色调值。我的目标是使整个图像看起来颜色相同,而不会失去任何区域的亮度。我找到了一种使用 ColorMatrix 来移动图像的色调值的方法
我最近将我的 Postgres 数据库从 Windows 迁移到 CentOS 6.7。在 Windows 上,数据库从不使用太多 CPU,但在 Linux 上,我看到它使用恒定的 ~30% CPU(
你好,我正在用 SDL2 开发一款很棒的游戏,我添加了一个 FPS 调节器以使其在每台机器上都能流畅运行,但后来我创建了一个示例程序来输出 fps(不是调节,只是输出以查看电流的功率机器运行 ) 我在
我在 IntelliJ IDEA 13.0 中开发了一个应用程序,但是当我在我的设备上运行该应用程序时,它会不断等待调试器(如果我在 Debug模式下运行,调试器连接时没问题)但是如果远离计算机,我无
我想在 stat_密度2 图中绘制多个组,其中 alpha 值与每组中的观察计数相关。然而,由 stat_密度2d 形成的水平似乎已标准化为每组中的观察数量。例如, temp <- rbind(mov
我是一名优秀的程序员,十分优秀!