- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两张图显示供求关系,还有一张图,我从供应中减去需求以显示由此产生的不对称。我想对 x 轴和不对称负部分之间的区域进行阴影处理,以显示缺陷的程度。
我目前使用以下代码:
plot.asymmetry <- ggplot(data=df.overview.month,
aes(x=Date.Time, y=Asymmetry)) +
geom_area(data=subset(df.overview.month, Asymmetry < 0),
aes(x=Date.Time, y=Asymmetry))
time.initial <- as.POSIXct("2010-12-31 23:00:00", tz="GMT")
Date.Time<-vector()
for(i in 1:24) {
Date.Time[i] <- time.initial + i*3600
}
Demand<-vector()
for(i in 0:23) {
Demand[i+1] <- 155 + 20*sin((pi/12)*i - (pi/2)) + 10*sin((pi/4380)*i + (pi/2))
}
Supply<-vector()
for(i in 0:23) {
Supply[i+1] <- 165 + 5*sin((pi/4380)*i - (pi/2)) + rnorm(1, mean=0, sd=0.20*165)
}
df.overview.month <- data.frame(Date.Time, Demand, Supply, Asymmetry=Supply-Demand)
最佳答案
下面是一些代码 ported from Matlab计算线段之间的交集。如果将它应用在 x 轴(固定)和每对连续点之间,您会得到一个新坐标列表,这些坐标表示 geom_line
之间的交叉点。和 x 轴。由此,对相关多边形进行着色是一个简单的步骤。请注意,我没有正确测试移植的 Matlab 代码。
## Ported from Matlab to R
## Copyright (c) 2010, U. Murat Erdem
## All rights reserved.
## http://www.mathworks.com/matlabcentral/fileexchange/27205
lineSegmentIntersect <- function(XY1, XY2){
n_rows_1 <- nrow(XY1)
n_cols_1 <- ncol(XY1)
n_rows_2 <- nrow(XY2)
n_cols_2 <- ncol(XY2)
stopifnot(n_cols_1 == 4 && n_cols_2 == 4)
nc <- n_rows_1 * n_rows_2
X1 <- matrix(XY1[,1], nrow=nc, ncol=1)
X2 <- matrix(XY1[,3], nrow=nc, ncol=1)
Y1 <- matrix(XY1[,2], nrow=nc, ncol=1)
Y2 <- matrix(XY1[,4], nrow=nc, ncol=1)
XY2 <- t(XY2)
X3 <- matrix(XY2[1,], nrow=nc, ncol=1)
X4 <- matrix(XY2[3,], nrow=nc, ncol=1)
Y3 <- matrix(XY2[2,], nrow=nc, ncol=1)
Y4 <- matrix(XY2[4,], nrow=nc, ncol=1)
X4_X3 <- X4-X3
Y1_Y3 <- Y1-Y3
Y4_Y3 <- Y4-Y3
X1_X3 <- X1-X3
X2_X1 <- X2-X1
Y2_Y1 <- Y2-Y1
numerator_a <- X4_X3 * Y1_Y3 - Y4_Y3 * X1_X3
numerator_b <- X2_X1 * Y1_Y3 - Y2_Y1 * X1_X3
denominator <- Y4_Y3 * X2_X1 - X4_X3 * Y2_Y1
u_a <- numerator_a / denominator
u_b <- numerator_b / denominator
INT_X <- X1 + X2_X1 * u_a
INT_Y <- Y1 + Y2_Y1 * u_a
INT_B <- (u_a >= 0) & (u_a <= 1) & (u_b >= 0) & (u_b <= 1)
PAR_B <- denominator == 0
COINC_B <- (numerator_a == 0 & numerator_b == 0 & PAR_B)
data.frame(x=INT_X[INT_B], y=INT_Y[INT_B])
}
set.seed(123)
x <- sort(runif(50, -10, 10))
y <- jitter(sin(x), a=2)
n <- length(x)
xy1 <- matrix(c(-10, 0, 10, 0), ncol=4)
xy2 <- cbind(x[-n], y[-n], x[-1], y[-1])
test <- lineSegmentIntersect(xy1, xy2)
library(ggplot2)
d <- data.frame(x=x, y=y)
d2 <- rbind(d, test)
d2 <- subset(d2[order(d2$x), ], y <=0)
p <- qplot(x, y, data=d, geom="path")
p + geom_ribbon(data=d2, aes(ymin = 0, ymax = y), fill="red")
关于r - 对 geom_line 和 x 轴之间的区域进行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973381/
在 geom_line 中获得奇怪的顶点排序阴谋。左图是以 R 为底的;正确的是ggplot。 这是shapefile我正在与。这将重现情节: require(ggplot2); require(ma
我正在融化一个数据框,以便在 ggplot2 中绘制它。然而,geom_line()没有给我水平线。相反,它正在将我的点转换为垂直线。我将附上图片并添加代码以进一步说明: 仅使用 geom_point
我想根据两个变量的相互作用对数据进行group编码,但只能将美观性映射到这些变量之一。 (另一个变量表示理论上应该等效的重复项)。我可以找到实现此目的的优雅方法,但似乎应该有一种更优雅的方法。 例如
有没有办法抖动 geom_line() 中的线条? ?我知道这有点违背了这个情节的目的,但是如果你有一个只有几行的情节并且希望他们都展示它可能会很方便。也许有一些其他解决方案可以解决这个可见性问题。
您好,我有一个关于 geom_line 的问题。 我想要做的是,对于同一条线,根据应用于另一个变量的某些过滤器具有不同的颜色段,即不是用于绘制线的变量。 例如,如果我们有一个带有标题时间、var1 和
我有一个图,其中每个点都有点、线和注释。问题是线条最终会穿过注释文本,我想找到一个解决方案来避免这种情况,例如如何使注释位于“顶部”? 我的伪 ggplot 就像: # some df ggplot(
我有一个图,其中每个点都有点、线和注释。问题是线条最终会穿过注释文本,我想找到一个解决方案来避免这种情况,例如如何使注释位于“顶部”? 我的伪 ggplot 就像: # some df ggplot(
我正在尝试使用 plotly 构建交互式图表,但它无法显示 geom_line(),我不确定为什么。这不是我的确切示例,但即使是 plotly 网页上提供的示例也失败了。在这里: library(pl
我使用 ggplot 和 geom_line 创建了一个绘图,并希望在绘图上显示单个数据值(数字)。 这是我目前的代码: ggplot(errors_prob_3) + geom_point(aes(
我有要绘制的数据。我有几个变量,范围从 1880 年到 2012 年。我每年观察一次。但有时一个变量多年没有观测值。例如,它可能有 1880-1888 年的观测值,但没有 1889-1955 年和 1
这是我一直在尝试寻找修复方法的事情,但基本上我想知道是否有一种快速的方法来“躲避”ggplot2中两个不同数据集的线图。 我的代码目前是: #Example data id <- c("A","A")
这是我要修改的绘图的数据: mdfr con,header=T);close(con) 我使用以下命令创建了一个绘图 ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y
我有以下要绘制的数据框。我想知道是否可以将连接我的结果变量(stackOne$y)的部分线条着色为不同的颜色,具体取决于它是否小于某个值。例如,我希望低于 2.2 的线条部分为红色。 set.seed
我有这个数据框和这个图: df <- data.frame(Groupe = rep(c("A","B"),4), Period = gl(4,2,8,c("t0",
我编写了以下代码: library(ggplot2) data <- structure(list(x = c(1L, 6L, 3L, 4L, 2L, 3L, 6L, 1L, 5L, 2L,
我想用ggplot2建立一个情节。因此,我使用geom_line来可视化线,并使用geom_smooth来显示特定索引的最小-最大范围。 使用了两个数据框,第一行包含日期(例如:2013-02-04)
ggplot(mtcars, aes(wt, mpg)) + geom_line() + geom_point() geom_line() 用一条直线连接点,这是两个点之间的最短路径。我希望这条线首先
我正在尝试做与这篇文章类似的事情 here ,用箭头连接基于分组(在本例中为站点)的点,并根据特定值(年份)指定箭头方向。我无法让箭头的方向正常工作。我想让箭头的方向始终从 2017 年到 2018
我正在尝试创建一个类似于下图的图,其中蓝色遮盖了灰色。然而,在我正在创建的情节中,蓝色并没有掩盖其他颜色。有人可以指导我如何实现吗? 这是我想得到的颜色图: 这是我的情节: ggplot(df,
我正在尝试手动更改 ggplot2 线图中图例的第一项的颜色。 我对一个变量有几个观察结果,我在一个线图中显示,就像这样: ggplot(tmp1, aes(x=factor(month), y=va
我是一名优秀的程序员,十分优秀!