Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当x是一个因子时,组合geom_ribbon_R_Ggplot2 - Fatal编程技术网

当x是一个因子时,组合geom_ribbon

当x是一个因子时,组合geom_ribbon,r,ggplot2,R,Ggplot2,从这里开始 当myx是一个因子时,我无法生成阴影区域。 下面是一些示例数据 time <- as.factor(c('A','B','C','D')) x <- c(1.00,1.03,1.03,1.06) x.upper <- c(0.91,0.92,0.95,0.90) x.lower <- c(1.11,1.13,1.17,1.13) df <- data.frame(time, x, x.upper, x.lower) ggplot(data = df,

从这里开始

当my
x
是一个因子时,我无法生成阴影区域。 下面是一些示例数据

time <- as.factor(c('A','B','C','D'))
x <- c(1.00,1.03,1.03,1.06)
x.upper <- c(0.91,0.92,0.95,0.90)
x.lower <- c(1.11,1.13,1.17,1.13)

df <- data.frame(time, x, x.upper, x.lower)

ggplot(data = df,aes(time,x))+
  geom_ribbon(aes(x=time, ymax=x.upper, ymin=x.lower), fill="pink", alpha=.5) +
  geom_point()

我仍然无法得到阴影。任何克服这个问题的方法…

我认为aosmith是完全正确的,您只需要将因子变量转换为数值。我认为以下代码就是您要查找的代码:

ggplot(data = df,aes(as.numeric(time),x))+
  geom_ribbon(aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower),     
  fill="pink", alpha=.5) +
  geom_point()
它生成了这个图:

编辑:将x轴标签更改回其原始值,该值取自以下注释中的@aosmith:

ggplot(data = df,aes(as.numeric(time),x))+
  geom_ribbon(aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower),     
  fill="pink", alpha=.5) +
  geom_point() + labs(title="My ribbon plot",x="Time",y="Value") +
  scale_x_continuous(breaks = 1:4, labels = levels(df$time))

我相信
geom_ribbon
仅适用于连续x值。解决方法是将因子视为连续(
视为.numeric(time)
),然后使用
scale\u x\u continuous
更改x轴的打断/标签。您能否建议一种解决方案,我想可能是OP要求轴刻度标签。类似于
scale\u x\u continuous(中断=1:4,标签=levels(df$time))
ggplot(data = df,aes(as.numeric(time),x))+
  geom_ribbon(aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower),     
  fill="pink", alpha=.5) +
  geom_point() + labs(title="My ribbon plot",x="Time",y="Value") +
  scale_x_continuous(breaks = 1:4, labels = levels(df$time))