我的数据:
day variable value
1 Fri avg1 446.521127
2 Mon avg1 461.676056
3 Sat avg1 393.366197
4 Sun avg1 435.985714
5 Thu avg1 445.571429
6 Tue avg1 441.549296
7 Wed avg1 462.042254
8 Fri avg2 7.442113
9 Mon avg2 7.694648
10 Sat avg2 6.556056
11 Sun avg2 7.266571
12 Thu avg2 7.426286
13 Tue avg2 7.359577
14 Wed avg2 7.700282
Run Code Online (Sandbox Code Playgroud)
我的问题是我想facet_grid用白天显示每组平均数据创建一个条形图,但观察结果相似,我发现使用指定y限制很有帮助scale_y_continuous.
所以,如果我分配我的ggplot g <- ggplot(df, aes(x=day, y=value)),我可以得到我想要的一半:
g + geom_bar(stat="identity") + facet_grid(variable~., scales="free")
Run Code Online (Sandbox Code Playgroud)
和
g …Run Code Online (Sandbox Code Playgroud) 我看到了这个答案,但无法复制它.
我得到这样的数据:
df = data.frame(x = rep(sample(letters, 4), 2),
y = round(runif(8,1,100),0),
z = c(rep("group1",4), rep("group2",4)))
# I then add a 'percent' column like so:
df$perc[1:4] = df$y[1:4] / sum(df$y[1:4])
df$perc[5:8] = df$y[5:8] / sum(df$y[5:8])
# Which I then convert like so:
df$perc = paste(round(df$perc * 100, 1), "%", sep="")
# The ggplot:
library(ggplot2)
ggplot(df) +
geom_bar(aes(z, y, fill=x), position="dodge", stat="identity") +
geom_text(aes(z,y,label=perc), position=position_dodge(width=1), size=4)
Run Code Online (Sandbox Code Playgroud)
结果:

我无法弄清楚我做错了什么.