有没有办法geom_bar()在下面的时间序列示例中丢失数据的情况下设置恒定宽度?我试过设置width在aes()没有运气.在代码示例下方的图中比较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)

我正在制作一个条形图,显示返回嵌套的不同类型的猎物的百分比。
我的数据如下:
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) 考虑一些未发生级别的示例数据:
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)
在样本数据中,男性不能怀孕,因此他们没有数据.
我想用绘制数据ggplot2在R使用此代码:
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)
我得到这个输出:
正如您所看到的,女性和怀孕的酒吧比其他酒吧更宽.我的问题是如何抑制条形加宽以使条形宽度相同?
这是我的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 …