- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 y 轴上有一个连续变量,在 x 轴上有一个分类变量。在分类变量中,顺序是有意义的,并且通过其索引来拟合回归也是有意义的,我的意思是使用索引而不是 c('a', 'b', 'c')
(order(c('a', 'b', 'c'))
,即 c(1, 2, 3)
),并根据模型拟合这。但是,如果一个变量不是数字,则 ggplot 会拒绝拟合 geom_smooth(method = lm)
。好的,那么我可以告诉它使用命令:
geom_smooth(aes(x = order(hgcc), y = rtmean), method = lm)
但是它会从数据框中获取整个列的索引,当只有 级别的子集时,这对于使用
变量出现在一张图上。整个数据框中的索引平均要高得多,因此回归将绘制在最右侧:scales = 'free'
进行分面来说并不好。 x
这是一个最小的工作示例:
require(ggplot2)
load(url('http://www.ebi.ac.uk/~denes/54b510889336eb2591d8beff/sample_data.RData'))
ggplot(adata12cc, aes(x = hgcc, y = rtmean, color = cls, size = log10(intensity))) +
geom_point(stat = 'sum', alpha = 0.33) +
geom_smooth(
aes(x = order(hgcc), y = rtmean),
method = 'glm') +
facet_wrap( ~ uhgroup, scales = 'free') +
scale_radius(guide = guide_legend(title = 'Intensity (log)')) +
scale_color_discrete(guide = guide_legend(title = 'Class')) +
xlab('Carbon count unsaturation') +
ylab('Mean RT [min]') +
ggtitle('RT vs. carbon count & unsaturation by headgroup') +
theme(axis.title = element_text(size = 24),
axis.text.x = element_text(angle = 90, vjust = 0.5, size = 9, hjust = 1),
axis.text.y = element_text(size = 11),
plot.title = element_text(size = 21),
strip.text = element_text(size = 18),
panel.grid.minor.x = element_blank())
我知道这不是一个很好的做事方式,但是如果我可以引用这些变量并用它们做一些事情,那么 ggplot 可以让生活变得更容易,这些变量无论如何都是通过分面进行子集化的。
最佳答案
我想我已经找到了解决方案,但我不确定你想要什么......
主要问题是您的 x 值标签已被 uhgroup
分割如果你看看这个因素,它们是 PC-O(38.7)
PC(38.7
等...
因此,第一件事就是为 x 轴创建一个新的 hgcc
值。
adata12cc$hgcc_value <-as.factor(substr(adata12cc$hgcc, (nchar(levels(adata12cc$hgcc)[adata12cc$hgcc])-5), nchar(levels(adata12cc$hgcc)[adata12cc$hgcc])))
另一个问题是 geom_point
和 geom_smooth
有不同的 x 轴。一个是hgcc
,另一个是order(hgcc_value)
。
解决方案是使用相同的值,这里我使用 as.numeric(hgcc_value)
(而不是 order()
)并在 scale_x_continuous 中精确
中断的标签。
ggplot(adata12cc, aes(x = as.numeric(hgcc_value), y = rtmean, color = cls, size = log10(intensity))) +
geom_point(stat = 'sum', alpha = 0.33) +
geom_smooth(
aes(x = as.numeric(hgcc_value), y = rtmean),
method = 'glm') +
facet_wrap( ~ uhgroup, scales = 'free') +
scale_radius(guide = guide_legend(title = 'Intensity (log)')) +
scale_color_discrete(guide = guide_legend(title = 'Class')) +
scale_x_continuous(name = "Carbon count unsaturation",
breaks=as.numeric(adata12cc$hgcc_value),
labels = adata12cc$hgcc_value,
minor_breaks = NULL)+
ylab('Mean RT [min]') +
ggtitle('RT vs. carbon count & unsaturation by headgroup') +
theme(axis.title = element_text(size = 24),
axis.text.x = element_text(angle = 90, vjust = 0.5, size = 9, hjust = 1),
axis.text.y = element_text(size = 11),
plot.title = element_text(size = 21),
strip.text = element_text(size = 18),
panel.grid.minor.x = element_blank())
这是您要找的吗?
关于r - ggplot2:适合 geom_smooth() 像分类变量是连续的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40741068/
出于某种原因,在我的图表中,geom_smooth 线的颜色与阴影区域不同。我不确定是什么原因造成的。例如,蓝色阴影的 Retired 有一条绿线,而绿色阴影的 Unsigned 有一条蓝线。事件状态
我有以下 ggplot2 代码,可以绘制不同阶数的多个多项式拟合: library(ggplot2) set.seed(1234) n = 400 x = rnorm(n, sd=0.4) y = -
如何制作 ggplot 图 geom_smooth(method="lm"),但前提是它符合某些标准?例如,如果我只想在斜率具有统计显着性的情况下画线(即 lm 拟合中的 p 小于 0.01)。 编辑
我在 R 中计算了二项式回归: Call: glm(formula = cbind(success, failure) ~ x * f, family = "binomial", data =
这是一些数据和情节: set.seed(18) data = data.frame(y=c(rep(0:1,3),rnorm(18,mean=0.5,sd=0.1)),colour=rep(1:2,1
我正在使用 ggplot2 中的 geom_smooth()。 在 Hadley Wickham 的书(“ggplot2 - Elegant Graphics for Data Analysis”)中
您可以非常轻松地在 ggplot2 中扩展回归线: c =init_range[1]] ggplot2:::predictdf.default(model, xseq[-length(xseq
在查看this时问题,我无法为 geom_smooth 指定自定义线性模型。我的代码如下: example.label <- c("A","A","A","A","A","B","B","B","B"
我正在努力使用 geom_smooth 来创建几何平滑线。下面我报告代码: library(ggplot2) #DATAFRAME RawData <- data.frame("Time" = c(0
是否可以让geom_smooth产生单调递减函数? 第一个例子看起来是单调递减的: library(tidyverse) df <- structure(list(x = c(-55, 11, 19,
这个问题在这里已经有了答案: ggplot - Add regression line on a boxplot with binned (non-continuous) x-axis (1 个回答)
我正在绘制一些数据并具有以下代码: ggplot(aes(x = x, y = y), data = data) + geom_point(alpha = 1/15, color = 'blue'
我想用ggplot2建立一个情节。因此,我使用geom_line来可视化线,并使用geom_smooth来显示特定索引的最小-最大范围。 使用了两个数据框,第一行包含日期(例如:2013-02-04)
如果我添加 geom_smooth,那么我会在 shape 图例中得到不同颜色的矩形,而不是黑色圆圈。我怎样才能防止这种情况发生?这是 sample code . library(ggplot2) d
我正在寻找两个变量之间的比较图,重叠两个产品的 geom_smooth()。测量力理论的起点是 (x=0; y=0) 但是当我制作图表时,蓝色回归线是用 geom_smooth() 不传递坐标 (0;
如何在 geom_smooth() 中改变线条边框的颜色? library(ggplot2) mtcars$cyl <- as.factor(mtcars$cyl) ggplot(mtcars, ae
我有如下数据: library(quantreg) library(ggplot2) data <- structure(list(country_mean_rep = structure(c(73.
这个问题在这里已经有了答案: R stat_smooth all points (1 个回答) 关闭4年前。 我正在绘制一个包含 3 个不同类别的图表,这些类别由不同的颜色表示。我想要一条曲线来表示总
我有一个小问题,我自己无法解决这个问题。 我有一个简单的数据框,我想用 ggplot2 绘制它.当我使用变量 重量 作为一个因素,我得到 x 轴上的所有值,s。 plot 2,但当我将它用作整数时,s
使用以下代码: library(ggplot2) ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(aes(colour=factor(cyl)))
我是一名优秀的程序员,十分优秀!