Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
R ggplot错误条图例_R_Ggplot2_Legend - Fatal编程技术网

R ggplot错误条图例

R ggplot错误条图例,r,ggplot2,legend,R,Ggplot2,Legend,我在错误条图中添加图例时遇到困难。我尝试了几个我在其他主题中见过的命令,但不幸的是它不起作用(我确信我遗漏了一些东西,但我不知道是什么) 问题在于您使用的是宽格式数据,而不是长格式数据。如果要获得图例,则需要在打印前将数据从宽转换为长 library(ggplot2) errors=matrix(c(-3.800904,-3.803444,-3.805985,-3.731204,-3.743969, -3.756735,-3.742510,-3.764961,-3

我在错误条图中添加图例时遇到困难。我尝试了几个我在其他主题中见过的命令,但不幸的是它不起作用(我确信我遗漏了一些东西,但我不知道是什么)


问题在于您使用的是宽格式数据,而不是长格式数据。如果要获得图例,则需要在打印前将数据从宽转换为长

library(ggplot2)
errors=matrix(c(-3.800904,-3.803444,-3.805985,-3.731204,-3.743969,
                -3.756735,-3.742510,-3.764961,-3.787413,-3.731204,-3.743969,-3.756735,
                -3.711420,-3.721589,-3.731758,-3.731204,-3.743969,-3.756735,-3.636346,
                -3.675159,-3.713971,-3.731204,-3.743969,-3.756735),nrow=4,byrow=TRUE)

errors = rbind(errors[, 1:3], errors[,4:6]) # manually reshaping the data
modelName=c("model 1","model 2","model 3","model 0")
type = rep(c("model", "obs"), each = 4)

boxdata=data.frame(errors,modelName, type)

colnames(boxdata)=c("icp","pred","icm","model", "type")

ggplot(boxdata, aes(x = model, y = pred, ymax = icp, ymin = icm, 
                    group = type, colour = type, shape = type)) +
  geom_errorbar(width=0.20) +
  geom_point() +
  scale_shape_manual(values=c(19, 4)) +
  scale_color_manual(values = c("black","orange")) +
  xlab("models") + 
  ylab("confidence level")
输出看起来更接近您的输出,可通过以下方式生成:

ggplot(boxdata, aes(x = model, y = pred, ymax = icp, ymin = icm, 
                    group = type, colour = type, shape = type)) +
  geom_errorbar(width=0.20) +
  geom_point(colour =  rep(c("black","orange"), each = 4)) +
  scale_shape_manual(values=c(19, 4)) +
  scale_color_manual(values = c("deepskyblue", "red")) +
  xlab("models") + 
  ylab("confidence level")

问题是您使用的是宽格式数据,而不是长格式数据。如果要获得图例,则需要在打印前将数据从宽转换为长

library(ggplot2)
errors=matrix(c(-3.800904,-3.803444,-3.805985,-3.731204,-3.743969,
                -3.756735,-3.742510,-3.764961,-3.787413,-3.731204,-3.743969,-3.756735,
                -3.711420,-3.721589,-3.731758,-3.731204,-3.743969,-3.756735,-3.636346,
                -3.675159,-3.713971,-3.731204,-3.743969,-3.756735),nrow=4,byrow=TRUE)

errors = rbind(errors[, 1:3], errors[,4:6]) # manually reshaping the data
modelName=c("model 1","model 2","model 3","model 0")
type = rep(c("model", "obs"), each = 4)

boxdata=data.frame(errors,modelName, type)

colnames(boxdata)=c("icp","pred","icm","model", "type")

ggplot(boxdata, aes(x = model, y = pred, ymax = icp, ymin = icm, 
                    group = type, colour = type, shape = type)) +
  geom_errorbar(width=0.20) +
  geom_point() +
  scale_shape_manual(values=c(19, 4)) +
  scale_color_manual(values = c("black","orange")) +
  xlab("models") + 
  ylab("confidence level")
输出看起来更接近您的输出,可通过以下方式生成:

ggplot(boxdata, aes(x = model, y = pred, ymax = icp, ymin = icm, 
                    group = type, colour = type, shape = type)) +
  geom_errorbar(width=0.20) +
  geom_point(colour =  rep(c("black","orange"), each = 4)) +
  scale_shape_manual(values=c(19, 4)) +
  scale_color_manual(values = c("deepskyblue", "red")) +
  xlab("models") + 
  ylab("confidence level")

为了获得图例,您需要将颜色映射到变量。不要在
aes
内部使用
$
。Bravo用于生成解释错误条含义的图形。为了获得图例,您需要将颜色映射到变量。不要在
aes
内部使用
$
。Bravo用于生成解释错误条含义的图形意思是。