小编Fbj*_*506的帖子

更改ggplot上的geom_bar宽度

我正在制作一个条形图,显示返回嵌套的不同类型的猎物的百分比。

我的数据如下:

Prey <- c(rep("Bird", 12), rep("Lizard", 3), rep("Invertebrate", 406))  
Type <- c(rep("Unknown bird", 12), rep("Skink", 2), rep("Gecko", 1), 
          rep("Unknown Invertebrate", 170), rep("Beetle", 1), 
          rep("Caterpillar", 3), rep("Grasshopper", 3), rep("Huhu grub", 1),  
          rep("Moth", 34), rep("Praying mantis", 1), rep("Weta", 193))  
Preydata <- data.frame(Prey,Type)  

ggplot(Preydata, aes(x = Prey, y = (..count..)/sum(..count..))) +
                  scale_y_continuous(labels = percent_format()) + 
                  geom_bar(aes(fill = Type), position = "dodge")
Run Code Online (Sandbox Code Playgroud)

我的图形如下图所示。

在此处输入图片说明

我希望所有“类型”栏的宽度都相同,但是当我更改其下的宽度时geom_bar,只会更改“猎物”栏的宽度。当我尝试使用以下内容时:

ggplot(Preydata, aes(x = as.numeric(interaction(Prey, Type)), 
    y = (..count..)/sum(..count..))) + 
    scale_y_continuous(labels = percent_format()) + 
    geom_bar(aes(fill = Type), position …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
1
解决办法
1425
查看次数

GAM模型错误

我的数据框如下所示:

head(bush_status)
distance  status count
       0 endemic   844
       1 exotic     8
       5 native     3
      10 endemic    5
      15 endemic    4
      20 endemic    3
Run Code Online (Sandbox Code Playgroud)

计数数据是非正态分布的。我试图以两种方式将广义加性模型拟合到我的数据中,以便可以使用方差分析来查看p值是否支持m2。

m1 <- gam(count ~ s(distance) + status, data=bush_status, family="nb")
m2 <- gam(count ~ s(distance, by=status) + status, data=bush_status, family="nb")
Run Code Online (Sandbox Code Playgroud)

m1工作正常,但是m2发送错误消息:

"Error in smoothCon(split$smooth.spec[[i]], data, knots, absorb.cons, 
scale.penalty = scale.penalty,  : 
  Can't find by variable"
Run Code Online (Sandbox Code Playgroud)

这远远超出了我,因此如果有人可以提供任何建议,将不胜感激!

r gam glm mgcv

2
推荐指数
1
解决办法
1175
查看次数

标签 统计

r ×2

gam ×1

ggplot2 ×1

glm ×1

mgcv ×1