相关疑难解决方法(0)

如果数据丢失,geom_bar的宽度一致

有没有办法geom_bar()在下面的时间序列示例中丢失数据的情况下设置恒定宽度?我试过设置widthaes()没有运气.在代码示例下方的图中比较5月'11至6月'11的条形宽度.

colours <- c("#FF0000", "#33CC33", "#CCCCCC", "#FFA500", "#000000" )
iris$Month <- rep(seq(from=as.Date("2011-01-01"), to=as.Date("2011-10-01"), by="month"), 15)

colours <- c("#FF0000", "#33CC33", "#CCCCCC", "#FFA500", "#000000" )
iris$Month <- rep(seq(from=as.Date("2011-01-01"), to=as.Date("2011-10-01"), by="month"), 15)
d<-aggregate(iris$Sepal.Length, by=list(iris$Month, iris$Species), sum)
d$quota<-seq(from=2000, to=60000, by=2000)
colnames(d) <- c("Month", "Species", "Sepal.Width", "Quota")
d$Sepal.Width<-d$Sepal.Width * 1000
g1 <- ggplot(data=d, aes(x=Month, y=Quota, color="Quota")) + geom_line(size=1)
g1 + geom_bar(data=d[c(-1:-5),], aes(x=Month, y=Sepal.Width, width=10, group=Species, fill=Species), stat="identity", position="dodge") + scale_fill_manual(values=colours)
Run Code Online (Sandbox Code Playgroud)

情节

r ggplot2

44
推荐指数
3
解决办法
2万
查看次数

更改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
查看次数

当存在冗余级别时,如何在R的ggplot2中保持恒定宽度条形图?

考虑一些未发生级别的示例数据:

dat <- data.frame(sex = c("F","M","F"),
                  status = c("Pregnant","Not pregnant","Not pregnant"),
                  frequency = c(25,100,75))
Run Code Online (Sandbox Code Playgroud)

在样本数据中,男性不能怀孕,因此他们没有数据.

我想用绘制数据ggplot2R使用此代码:

library(ggplot2)

p <- ggplot(dat,aes(x=status,y=frequency,fill=sex))+
  geom_bar(stat = "identity",position="dodge")

print(p)
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

在此输入图像描述

正如您所看到的,女性和怀孕的酒吧比其他酒吧更宽.我的问题是如何抑制条形加宽以使条形宽度相同?

r ggplot2

4
推荐指数
2
解决办法
1052
查看次数

在count = 0的情况下,如何使用geom_bar()去除ggplot2图的较宽条

这是我的data.frame():

df <- data.frame(Round = rep(c("A", "B", "C" ),150), Group = rep(c("UP", "DOWN"),75),Task = rep(c("T1", "T2", "T3", "T4", "T5"),30), V2 = sample(c(0,1,2), 50, replace = T), V1 = sample(c(0,1,2), 50, replace = T))
dfmelt <- melt(df)
Run Code Online (Sandbox Code Playgroud)

我想尝试这样的情节facet_grid:

b <- ggplot(data=dfmelt, aes(x=value, fill=variable))
b <- b + geom_bar(stat="count", position = "dodge", width = 0.9)
b <- b + facet_grid(Group ~ Task, scales = "free")
Run Code Online (Sandbox Code Playgroud)

,产生以下内容:

在此输入图像描述

我想摆脱更广泛的列,例如V1 at the position 0 of T1-UP,V1 at the …

r ggplot2 geom-bar

3
推荐指数
1
解决办法
424
查看次数

标签 统计

ggplot2 ×4

r ×4

geom-bar ×1