我试图创建一个情节,我可以使用ggplot2在我的系数显着的点上填充该区域。
我创建了这个例子:
dt <- data.table(x = 0:23, y = c(0.00788665622373638, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.031263597681424, 0.0483478996438207,
0.0339161353262161, 0, 0, 0, 0, 0, 0, 0, 0), value = c(0.335524374372203,
0.310445022036626, 0.00348268861151579, 0.000645923627809575,
0.0025476114971974, 0.000979901982654185, 0.00447235816030944,
0.000375791689380511, 0.00850170357523439, 0.185246478252772,
0.236061996429638, 0.611479957550591, 0.916055517054685, 0.047195113633542,
0.00170024647583689, 0.0138696238231373, 0.700687775315984, 0.0562079029293676,
0.00527934454203627, 0.00870851100765857, 0.005848832805464,
0.00300379176492194, 0.00400049813928849, 0.323674152828656))
使用以下代码:
plt <- ggplot(dt,aes(x=x,y=y)) + geom_line(colour='blue') + geom_point() + geom_area(data=subset(dt,value<0.1 & y > 0),fill='skyblue',alpha=0.3)
我得到这张图:
似乎是连接值小于0.1的点,我只想为value
小于0.1的行下面的区域着色。
有什么方法吗?
答案 0 :(得分:1)
我一直在尝试提供一个可以转换数据的功能,以便根据请求进行绘制,这样做我发现了这个想法中存在的潜在问题。
考虑一个点x,其中y为正,值为&lt; 0.1,而x-1和x + 1的值> 0.1。使用geom_area时,由于线的面积为0,这一点将被忽略。因此,我相信其他几个可视化可能更有益:
geom_linerange或geom_pointrange可能更好(并且更容易绘制),这是您的数据的示例。它强调了值<&lt; 0.1和y> 0
ggplot(dt,aes(x=x,y=y)) +
geom_line(colour='blue') +
geom_point() +
geom_linerange(data = dt[dt$value < 0,1,], aes(ymin = 0, ymax = y), color= "skyblue", size = 1)
geom_point强调值&lt; 0.1
ggplot(dt,aes(x=x,y=y)) +
geom_line(colour='blue') +
geom_point() +
geom_point(data = dt[dt$value < 0.1,], color= "red", size = 2)
如果你真的开始使用geom_area这里是一个函数(只有基数R):
for_area = function(data, val){
df = data
v = ifelse(df$value >= val, 0, df$value)
y = ifelse(df$value >= val, 0, df$y)
df$value = v
df$y = y
pre = lapply(2:nrow(df), function(i){
pre = ifelse(df$y[i-1] == 0 & df$y[i] !=0, i, 0)
return(pre)
})
pro = lapply(1:nrow(df), function(i){
pro = ifelse(df$y[i] != 0 & df$y[i+1] ==0, i, 0)
return(pro)
})
pre = do.call(rbind, pre)
pro = do.call(rbind, pro)
pre = pre[pre>0]
pro = pro[pro>0]
pre = df$x[pre]
pro = df$x[pro]
df$x1 = 1
df = rbind(df, data.frame(x = pre,
y = rep(0, length(pre)),
value = rep(0, length(pre)),
x1 = rep(0, length(pre))))
df = rbind(df, data.frame(x = pro,
y = rep(0, length(pro)),
value = rep(0, length(pro)),
x1 = rep(2, length(pro))))
df = df[with(df, order(x, x1)),]
return(df)
}
使用op:
中的数据ggplot(dt,aes(x=x,y=y)) +
geom_line(colour='blue') +
geom_point() +
geom_area(data = for_area(dt, 0.1), fill= "skyblue", alpha = 0.3)
有一个更复杂的例子:
dput(daf)
structure(list(x = 1:25, y = c(0.3, 0.2, 0.2, 0, 0.1, 0.1, 0.3,
0.2, 0.3, 0.1, 0, 0.3, 0.2, 0.1, 0.3, 0, 0.2, 0.3, 0, 0.1, 0.1,
0.2, 0.3, 0, 0.3), value = c(0, 0.3, 0, 0, 0, 0.2, 0.3, 0.2,
0.2, 0.3, 0.2, 0.2, 0, 0, 0.2, 0, 0.2, 0, 0.1, 0.1, 0.1, 0, 0.3,
0.2, 0.3)), .Names = c("x", "y", "value"), row.names = c(NA,
-25L), class = "data.frame")
这说明了我之前提到的一些问题:x = 3处的值是0.0,而y = 0.2但是没有指示,因为x = 4且x = 2具有值&gt; 0.1 = y == 0
使用geom_pointrage,这将成为:
也许从两个世界中选择最好的:
ggplot(daf,aes(x=x,y=y)) +
geom_line(colour='blue') +
geom_point() +
geom_area(data = for_area(daf, 0.1), fill= "skyblue", alpha = 0.3 )+
geom_linerange(data = daf[daf$value<0.1,], aes(ymin = 0, ymax = y), color= "skyblue", size = 1)