- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我在自定义函数下调用 geom_hline 或 geom_vline 并且它从向量中获取值,我就会遇到问题。在我在该函数主体中添加 facet_grid() 之前,它似乎工作正常。例如无功能
c<- data.frame(A = c("carr","bike","truck","carr","truck","bike","bike","carr","truck","carr","truck","truck","carr","truck","truck"),
B = c(10,20,30,23,45,56,78,44,10,20,30,10,20,30,67),
D = c(1,2,3,1,2,3,2,3,2,3,2,2,3,2,1))
a = c(1:4)*4
ggplot(c, aes(A,B, color = D))+
geom_point()+
facet_grid( .~D)+
geom_hline(yintercept = a,linetype = "dotted",size =0.3)
`
但具有功能
tk_fun <- function(dat,x1,y1,clr){ # I need to have this a declared and defined with in function.
a = c(1:4)*4.5 p <- ggplot(dat, aes_string(colnames(dat)[1],colnames(dat)[2], color = colnames(dat)[3]))+
geom_point()+ facet_grid( .~dat[,3])+
geom_hline(yintercept = a,linetype = "dotted",size =0.3) return(p) } tk_fun(c,"A","B","D")
使用函数我得到这个错误:
Error in
$<-.data.frame
(*tmp*
, "PANEL", value = c(1L, 2L, 3L, 1L, : replacement has 15 rows, data has 4 I hope someone can help me in figuring out, how to do it through function, without an error. Thanks
最佳答案
问题在于您对构面的定义。您需要使用正确的变量名称创建适当的公式调用,而不是直接使用数据。使用 paste
和 as.formula
可以提供帮助。
tk_fun <- function(dat,x1,y1,clr){ # I need to have this a declared and defined with in function.
a = c(1:4)*4.5
p <- ggplot(dat, aes_string(colnames(dat)[1],colnames(dat)[2], color = colnames(dat)[3]))+
geom_point() +
facet_grid(as.formula(paste('. ~', names(dat)[3]))) +
geom_hline(yintercept = a, linetype = "dotted", size =0.3)
return(p)
}
关于r - geom_hline 或 geom_vline 似乎不接受矢量作为引用线,如果在函数内部调用并使用 facet_grid(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43978525/
我知道之前有人问过这个问题,但这些解决方案似乎对我不起作用。 我想要做的是用不同颜色在直方图上表示我的中位数、均值、上下分位数,然后在图中添加图例。这就是我到目前为止所拥有的,我尝试使用 scale_
我想在我的绘图上绘制几条垂直线,并为每个相应的 vline 提供一个图例。 阅读后this post ,这是我实现的: set.seed(99) df.size <- 1e6 my.df <- dat
我想在我的绘图上绘制几条垂直线,并为每个相应的 vline 提供一个图例。 阅读后this post ,这是我实现的: set.seed(99) df.size <- 1e6 my.df <- dat
对于下面的数据集,我想使用密度图来比较以下变量在两种性别中的分布:withdraw、affect 和 estress。这些图还应包含描述每个性别变量平均值的垂直线,并绘制在具有不同方面的同一图形上。
在查看了 StackOverflow 上提供的一些答案后,我一直试图添加一个额外的图例,但不知何故我无法完成这项工作。我正在使用以下代码: x_breaks <- seq(as.Date("2010
我试图扩展我的 ggplot 图中的 geom_vline 线,以超出绘图空间并进入轴区域。这样做的目的是让这些线分隔轴标签,以便它可以与旁边的另一个图对齐(见下文)。 一些简短的示例代码(我有更多的
我正在尝试添加应该特定于每个情节方面的垂直线。我试图通过与绘图数据分组相同的过程对 geom_vline 数据进行分组来做到这一点。垂直数据来自与点不同的数据帧。下面的代码行是我所拥有的。 plots
我使用分面 View 绘制了 3 个直方图,并为均值和中值添加了 vlines。 我想添加一个图例来指示哪个 vline 指的是哪个统计数据。 ggplot(x, aes(x=earnw)) + ge
我的情节中有这五 (5) 个 geom_vline(),我希望它们具有不同的颜色。有没有办法做到这一点? 这是我的代码, library(ggplot2) x <- seq(-7, 8, length
我无法在绘图的 x 轴上绘制离散(因子)级别的垂直线。在此解决方案中似乎有效 drawing vertical line with factor levels in ggplot2 但是 OTH 这不
我有一些 ggplot 代码在 0.8.9 中运行良好,但在 0.9.1 中不起作用。 我将绘制 theDF 中的数据并想在 xintercept="2010 Q1." 处绘制一条垂直线theGrid
我尝试在图表中包含一条垂直线,x 轴上有日期。线的位置应在某个日期之前确定。然而,我无法通过日期本身来控制位置,而只能通过数据集中日期的位置/行来控制。 由于其他行可能会在稍后的时间点进入数据集,因此
我四处寻找,但无法找到答案。我想做一个加权 geom_bar 图,上面覆盖着一条垂直线,显示每个方面的整体加权平均值。我无法做到这一点。垂直线似乎是应用于所有方面的单个值。 require('ggpl
我用 geom_hline() 和 geom_vline() 创建了一个散点图,该图很好,但图例条目不是我想要的显示方式。 vline (Restauration) 和 hline (Threshol
请看下面的例子 library(dplyr) library(lubridate) library(ggplot2) data data # A tibble: 5 × 3 time
与其他 ggplot geom 相比,geom_vline 似乎在色彩美学方面“表现不佳”。我想弄清楚我是否对 geom_vline 有误解,或者这是否是对 geom_vline 设计的疏忽。 # F
我使用 ggplot2 创建了一个混合密度图,我想在图中添加摘要信息,即每个密度的均值和代表某种真实均值的 1 个值。 这是从@Hernando Casas 借来的模拟示例。 set.seed(123
我有一个很大的时间序列数据集,我使用 xts 在 30 秒内进行了总结。不知道如何使这组易于重现,但它看起来像这样 > str(taonedf) 'data.frame': 480 obs. of
使用 geom_tile 函数时,我无法删除矩形之间的空白区域。 df <- data.frame( x = c(seq(2,16,2),seq(17,39,2)), y = c(rep(c(
我正在创建一个密度图并对其进行分面。假设我有 4 个变量组,使用这个变量对其进行分面将生成 4 个密度图。 对于每个密度图,我都画了一条代表平均值的垂直线。 然而,需要观察并查看 x 轴和垂直线之间的
我是一名优秀的程序员,十分优秀!