我无法在geom_plot中删除多余的空白侧翼组的酒吧。
我想做Roland在这里实现的目标:删除ggplot2条之间的空间,但是当我尝试实现他的解决方案时,出现错误“警告消息:
geom_bar()不再具有binwidth参数。请geom_histogram()改为使用。”
我将这行代码添加到绘图中(尝试不同的宽度):
geom_histogram(binwidth = 0.5) +
Run Code Online (Sandbox Code Playgroud)
它返回“错误:stat_bin()不能出于审美目的使用。” 而且没有情节。
数据:
mydf<- data.frame(Treatment = c("Con", "Con", "Ex", "Ex"),
Response = rep(c("Alive", "Dead"), times=2),
Count = c(259,10,290,21))
aPalette<-c("#009E73", "#D55E00")
Run Code Online (Sandbox Code Playgroud)
情节:
example<-ggplot(mydf, aes(factor(Response), Count, fill = Treatment)) +
geom_bar(stat="identity",position = position_dodge(width = 0.55), width =
0.5) +
scale_fill_manual(values = aPalette, name = "Treatment") + #legend title
theme_classic() +
labs(x = "Response",
y = "Count") +
scale_y_continuous(breaks = c(0,50,100,150,200,250,275), expand = c(0,0),
limits = c(0, 260)) …Run Code Online (Sandbox Code Playgroud) 我已经能够在所有构面之间添加垂直间隔(仅改变构面之间的水平间隔(ggplot2)),但是还无法在指定的构面之间仅添加一个间隔吗?
这是一个基于我的真实数据的示例(在真实情节中,我堆积了条形图):
mydf<-data.frame(year = rep(c(2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017),times = 2),
Area = rep(c('here','there'),times = 12),
yearArea = rep(c('here.2016','here.2017', 'there.2016','there.2017'), times = 12),
treatment = rep(c('control','control','control','treat', 'treat','treat'), times = 4),
response = rep(c('a','b','c','d'), times = 6),
count = rep(c(23,15,30,20), times = 6))
mycolour<-c("#999999", "#0072B2", "#009E73","#000000")
Run Code Online (Sandbox Code Playgroud)
返回图:
#default facet spacing
example<-ggplot(data=mydf, aes(x=treatment, y=count, fill=response)) +
geom_bar(stat="identity", width = 0.5) +
scale_fill_manual(values = mycolour, name = "Response") +
labs (y = "Count") +
facet_grid(~yearArea) +
theme_bw()
example
#spacing between each facet …Run Code Online (Sandbox Code Playgroud) 代码示例数据:
mydf<-data.frame(Group_ID=c("337", "337", "201", "201", "470", "470", "999", "999"),
Timestamp=c("A", "A", "B", "B", "C", "D", "E", "F"),
MU=c("1", "1", "2", "3", "4", "4", "5", "6"))
Run Code Online (Sandbox Code Playgroud)
示例数据框:
Group_ID Timestamp MU
1 337 A 1
2 337 A 1
3 201 B 2
4 201 B 3
5 470 C 4
6 470 D 4
7 999 E 5
8 999 F 6
Run Code Online (Sandbox Code Playgroud)
在“ Group_ID”中,我只想保留“ Timestamp”和“ MU”都不重复的条目。因此,在该示例中,将仅保留行7和8(“ Group_ID” 999具有“时间戳”和“ MU”两者的唯一条目)。
我的一些尝试:
mydf<-mydf %>%
group_by(Group_ID) %>%
filter(unique(Timestamp))
Run Code Online (Sandbox Code Playgroud)
返回错误:
“参数2过滤条件不等于逻辑向量”
如果这行得通,那么我将再次使用 unique(MU) …
示例数据:
mydf<-data.frame(Group_ID=c("337", "337", "201", "201", "470", "470", "999", "999"),
Timestamp=c("A", "A", "B", "B", "C", "D", "E", "F"),
MU=as.numeric(c("1", "1", "2", "3", "4", "4", "5", "6")))
Run Code Online (Sandbox Code Playgroud)
给出:
Group_ID Timestamp MU
337 A 1
337 A 1
201 B 2
201 B 3
470 C 4
470 D 4
999 E 5
999 F 6
Run Code Online (Sandbox Code Playgroud)
如果 MU 大于 1,我只想保留 Group_ID 中的第一个条目。如果 MU <= 1,我想保留该组的所有条目。因此,
想要的结果:
Group_ID Timestamp MU
337 A 1
337 A 1
201 B 2
470 C 4
999 E …Run Code Online (Sandbox Code Playgroud)