- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些 ggplot 功能,但我想给用户一个警告,以防他们严重滥用它。问题是这样的:如果有很多组并且组名很长,那么图例就会变得很大,占据了整个情节:
但如果我要更改情节名称,我会得到:
我所拥有的是:
library(ggplot2)
# stolen from https://ryouready.wordpress.com/2008/12/18/generate-random-string-name/
MHmakeRandomString <- function(n=1, lenght=12) {
randomString <- c(1:n)
for (i in 1:n)
{
randomString[i] <- paste(sample(c(0:9, letters, LETTERS),
lenght, replace=TRUE),
collapse="")
}
return(randomString)
}
makeData <- function(k, useLongNames = FALSE) {
x <- c(1, 100)
X <- cbind(1, x)
b <- matrix(rnorm(2*k), k, 2)
y <- numeric(2*k)
for (i in seq_len(k))
y[1:2 + 2*(i-1)] <- X %*% b[i, ]
df <- data.frame(x = c(1, n), y = y)
if (useLongNames) {
df$g <- factor(rep(MHmakeRandomString(k), each = 2))
} else {
df$g <- factor(rep(1:k, each = 2))
}
return(df)
}
# okayish plot
df <- makeData(50)
g0 <- ggplot(data = df, aes(x = x, y = y, group = g, color = g)) +
geom_line() +
guides(color=guide_legend(nrow=5))
# unreadable plot
df <- makeData(50, useLongNames = TRUE)
g1 <- ggplot(data = df, aes(x = x, y = y, group = g, color = g)) +
geom_line() +
guides(color=guide_legend(nrow=5))
# to plot
# x11(width = 480, height = 320)
# print(g0)
# x11(width = 480, height = 320)
# print(g1)
ggplotGrob()
中的某个地方.但是,我不熟悉 grobs(并且找不到明确的文档)并陷入困境
gGrob0 <- ggplotGrob(g0)
gGrob1 <- ggplotGrob(g1)
gGrob0$grobs[[15]]$grobs[[1]]$grobs # all legend elements
convertWidth(grobWidth(gGrob0$grobs[[15]]), unitTo = "inches") # 4.5128 inches
convertWidth(grobWidth(gGrob1$grobs[[15]]), unitTo = "inches") # 12.419 inches
# but this is not correct:
# number of legend columns x legend width <= plot width
# 10 * 12.419 <= 480
最佳答案
这是我的解决方案(受 m-dz 评论的启发)。对象 g0
和 g1
来自问题中的代码。
plotAndPrintRatio <- function(g, width, height) {
gGrob <- ggplotGrob(g)
tmpfile <- tempfile(pattern = "png")
png(tmpfile, width = width, height = height) # it is necessary to open a device
plot(g)
legendSize <- as.numeric(convertWidth(grobWidth(gGrob$grobs[[15]]), unitTo = "inches"))
plotSize <- as.numeric(convertWidth(grobWidth(gGrob$grobs[[7]]), unitTo = "inches"))
print(legendSize / plotSize) # the ratio of legend size to plot size
dev.off()
return(tmpfile)
}
# problem only in the first plot
f1 <- plotAndPrintRatio(g0, width = 480, height = 320) # 0.6769345
f2 <- plotAndPrintRatio(g1, width = 480, height = 320) # 1.887872 --> too big!
# larger window size fixes the problem
f3 <- plotAndPrintRatio(g0, width = 1200, height = 900) # 0.2707738
f4 <- plotAndPrintRatio(g1, width = 1200, height = 900) # 0.7551488
filesList <- list(f1, f2, f3, f4)
# to show the saved pngs:
dev.off() # might be needed to clean up the plotting window
grid::grid.raster(png::readPNG(f1))
grid::grid.raster(png::readPNG(f2))
grid::grid.raster(png::readPNG(f3))
grid::grid.raster(png::readPNG(f4))
# to remove the tempfiles created:
# lapply(filesList, file.remove)
关于r - 估计ggplot2中图例占用的绘图百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45794157/
我想计算或至少估计放置在相机/kinect 前面的物体的体积。知道我应该从哪里开始吗?你推荐 OpenCV 吗?您是否推荐任何其他技术,例如声纳/激光? 最佳答案 一直在用 OpenCV 2.3 编写
我想知道 MySQL 对表中总行数的 TABLE_ROWS 估计值是否有限制或保证误差范围? 最佳答案 如果它与 SHOW TABLE STATUS 发出的数字类似,则至少会偏差 +/- 40%,有时
我们都曾 mock 过“还剩 X 分钟”的对话框,它似乎过于简单,但我们如何改进它呢? 实际上,输入是截至当前时间的一组下载速度,我们需要使用它来估计完成时间,也许带有确定性指示,例如使用一些 Y%
我们都曾 mock 过“还剩 X 分钟”的对话框,它似乎过于简单,但我们如何改进它呢? 实际上,输入是截至当前时间的一组下载速度,我们需要使用它来估计完成时间,也许带有确定性指示,例如使用一些 Y%
我的理解是 glmnet 采用矩阵,其中每一列都是一个解释变量。 我有一个包含约 10 个解释变量的数据框(其中一些是因子) 我怎样才能使用诸如 y~(x1*x2*x3)+(x4*x5)+x6 之类的
有没有办法估计运行 R 的时间?命令而不实际运行它或仅部分运行命令? 我知道 system.time()存在但需要运行整个命令然后它给出了花费的时间。 最佳答案 还有http://www.ats.uc
在尝试使用 libGD 在 PHP 中调整图像大小之前,我想检查是否有足够的内存来执行操作,因为“内存不足”会完全杀死 PHP 进程并且无法被捕获。 我的想法是,原始图像和新图像中的每个像素 (RGB
我有一些 VHDL 文件,我可以在 Debian 上用 ghdl 编译它们。一些人已将相同的文件改编为 ASIC 实现。算法有一个“大面积”实现和一个“紧凑”实现。我想编写更多实现,但要评估它们,我需
我在 Amazon EC2 上使用 RStudio 0.97.320 (R 2.15.3)。我的数据框有 20 万行和 12 列。 我正在尝试使用大约 1500 个参数来拟合逻辑回归。 R 使用 7%
我目前正在估算一个新项目。假设只有一名开发人员在处理它,我的高水平估计是 25 周。 实际上会有两个开发人员并行工作。减少估计的什么因素是合理的? (我意识到不会是0.5) 最佳答案 根据原始开发人员
我试图更好地理解创建 Postgres 索引所涉及的权衡。作为其中的一部分,我很想了解通常使用多少空间索引。我已通读 the docs ,但找不到这方面的任何信息。我一直在做自己的小实验来创建表和索引
我对 Azure 平台相当陌生,需要一些有关 Azure 搜索服务成本估算的帮助。每个月我们都会有大约 500GB 的文件被放入 Azure Blob 存储中。我们希望仅根据文件名使用 Azure 搜
我正在尝试最大化横截面面板数据中的数据点数量。我的矩阵结构如下,y 轴为年份,x 轴为国家/地区: A B C D 2000 NA 50 NA
如果我有两个时间序列,例如: t f1 #[1] 0.25 #> f2 #[1] 0.25 f phase_difference #[1] 0.5 这意味着时间序列相移 pi/2,因为它们应该根据
我对 Azure 平台相当陌生,需要一些有关 Azure 搜索服务成本估算的帮助。每个月我们都会有大约 500GB 的文件被放入 Azure Blob 存储中。我们希望仅根据文件名使用 Azure 搜
我使用了以下 R 包:mice、mitools 和 pROC。 基本设计:3 个预测变量度量,在 n~1,000 的数据缺失率在 5% 到 70% 之间。 1 个二进制目标结果变量。 分析目标:确定
如何使用 lsmeans 来估计两个成对对比的差异?例如——想象一个连续的 dv 和两个因子预测变量 library(lsmeans) library(tidyverse) dat % fac
我制作了一个使用 BigDecimal 的科学计算器。它有一个特别消耗资源的功能:阶乘。现在,输入任何数字都会启动计算。根据运行此代码的设备,答案会在不同的时间显示。输入像 50000 这样的巨大值!
我已经发出了 sympy 命令来求解某个方程或另一个方程。现在已经好几天了,我不知道什么时候能完成。 我可以使用 sympy 来记录调用 .solvers.solve 的进度吗?如果不是,我如何估计
最近我得到了一些 error C6020: Constant register limit exceeded at variable; more than 1024 registers needed
我是一名优秀的程序员,十分优秀!