Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.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 使用ggplot2创建包含条形项目符号的条形图_R_Ggplot2_Geom Bar - Fatal编程技术网

R 使用ggplot2创建包含条形项目符号的条形图

R 使用ggplot2创建包含条形项目符号的条形图,r,ggplot2,geom-bar,R,Ggplot2,Geom Bar,我想使用ggplot2从一组数据中创建一个带有SDM的条形图($proteinN在Y中,$method在X中) 并将另一组数据($specific)和项目符号条形图的形状包含在同一个条形图(重叠)中,并在图例中显示一个指示器。 有点像这样(但垂直条和SDM用于第一组数据) (来源:) 这是我的代码和数据: library(ggplot2) data <- textConnection("proteinN, supp, method, specific 293,

我想使用ggplot2从一组数据中创建一个带有SDM的条形图($proteinN在Y中,$method在X中) 并将另一组数据($specific)和项目符号条形图的形状包含在同一个条形图(重叠)中,并在图例中显示一个指示器。 有点像这样(但垂直条和SDM用于第一组数据)


(来源:)

这是我的代码和数据:

    library(ggplot2) 
    data <- textConnection("proteinN, supp, method, specific
    293, protnumb, insol, 46
    259, protnumb, insol, 46
    274, protnumb, insol, 46
    359, protnumb, fasp, 49
    373, protnumb, fasp, 49
    388, protnumb, fasp, 49
    373, protnumb, efasp, 62
    384, protnumb, efasp, 62
    382, protnumb, efasp, 62
    ")

    data <- read.csv(data, h=T)

# create functions to get the lower and upper bounds of the error bars
stderr <- function(x){sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))}
lowsd <- function(x){return(mean(x)-stderr(x))}
highsd <- function(x){return(mean(x)+stderr(x))}

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", 
               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

# create a ggplot
ggplot(data=data,aes(x=method, y=proteinN, fill=method))+
  #Change _hue by _manualand remove c=45, l=80 if not desire#
  scale_fill_manual(values=cbPalette)+
  scale_fill_hue(c=45, l=80)+

  # first layer is barplot with means
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='black')+
  # second layer overlays the error bars using the functions defined above
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, 
              geom="errorbar", position="dodge",color = 'black', size=.5)
库(ggplot2)

数据可以试试这样的吗

ggplot(data=data,aes(x=method, y=proteinN, fill=method, width=1))+
  scale_fill_hue(c=45, l=80) +
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='black')+
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, 
               geom="errorbar", position="dodge",color = 'black', size=.5) + 
  geom_bar(data = unique(data[,c('method','specific')]),
           aes(x = method,y = specific),
           stat = "identity",
           fill = "light green",
           width = 0.5)
几张便条

你拼错了“宽度”

您的两行
缩放填充
是毫无意义的<代码>ggplot
将只采用一个填充比例,以最后出现的为准。你不能像那样“修改”填充比例。你应该得到一个警告,明确地说:

“填充”的比例已存在。为“填充”添加另一个比例,该比例将替换现有比例

您收到的错误消息说:

将变量映射到y并使用stat=“bin”

i、 e.您指定了
y=proteinN
,同时还使用
geom\u bar
中的
stat=“bin”
(默认设置)。它接着解释说:

使用stat=“bin”,它将尝试将y值设置为每组的病例数

i、 e.它不会在
y
中绘制值,而是会尝试计数(例如,
insol
)的实例数,并绘制该值。(本例中为三个。)粗略检查
?geom_bar
中的示例立即发现,大多数示例仅指定了x变量。在帮助中找到此示例之前:

# When the data contains y values in a column, use stat="identity"
library(plyr)
# Calculate the mean mpg for each level of cyl
mm <- ddply(mtcars, "cyl", summarise, mmpg = mean(mpg))
ggplot(mm, aes(x = factor(cyl), y = mmpg)) + geom_bar(stat = "identity")

或者只是提前将其拆分为自己的数据帧。

什么是“SDM”?你希望什么是外杆,什么是内杆?为此,您需要两个类别,但似乎有三种方法,因此我不确定您希望如何列出它们……此外,您至少可以完整地阅读错误消息。这是相当明确的至少一个问题,以及如何解决它。您是否至少尝试过遵循它的建议?很抱歉,SDM指的是平均值的标准偏差,我只希望列(proteinN)中的数据符合标准偏差。我确实试着按照错误消息中的建议去做,但是我对这种语言不是很熟悉,也不知道如何处理。非常感谢您的解释。我不明白geom_bar使用stat=“bin”作为默认值,结果是什么,现在我明白了。(不幸的是,我现在无法投票)现在我将尝试在我的传奇中整合新的酒吧(具体)。
# When the data contains y values in a column, use stat="identity"
library(plyr)
# Calculate the mean mpg for each level of cyl
mm <- ddply(mtcars, "cyl", summarise, mmpg = mean(mpg))
ggplot(mm, aes(x = factor(cyl), y = mmpg)) + geom_bar(stat = "identity")
unique(data[,c('method','specific')]