可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I was answering this question that I faced a strange problem;
I want to make a bar plot with ggplot2
and have x axis labels as the last character of Group
column. Use substring(Group, 3, 3)
for this purpose:
substring(df$Group, 3, 3)
# [1] "1" "2" "3" "1" "2" "1" "2" "3" "4"
But when I use it in ggplot
like below, it prints 1
instead of 4
at the last tick;
ggplot(data=df, aes(x=Group, y=Value)) +
geom_bar(stat="identity") +
scale_x_discrete(labels = substring(Group, 3, 3), expand=c(0.1,0.1)) +
facet_grid(~ substring(Group, 1, 1), space="free_x", scales="free_x", switch="x") +
theme_bw() +
theme(strip.placement = "outside",
strip.background = element_rect(fill=NA,colour="grey50"),
panel.spacing=unit(0,"cm"))
I can get it to work by using labels =unique(substring(Group, 3, 3)
but would someone explain what's going on?
Data:
df <- structure(list(Group = structure(1:9, .Label = c("1_1", "1_2",
"1_3", "2_1", "2_2", "3_1", "3_2", "3_3", "3_4"), class = "factor"),
Value = c(-1.23, 2.34, 0.56, 1.87, -2.4, 5.54, -0.98, -2.31,
6)), .Names = c("Group", "Value"), row.names = c(NA, -9L), class = "data.frame")
# > df
# Group Value
# 1 1_1 -1.23
# 2 1_2 2.34
# 3 1_3 0.56
# 4 2_1 1.87
# 5 2_2 -2.40
# 6 3_1 5.54
# 7 3_2 -0.98
# 8 3_3 -2.31
# 9 3_4 6.00
回答1:
I think that @Gregor's answer is the way to go, but I can explain the error. Even if you add df$
before group
, you get the behavior you described. You can see why if you don't let the scales vary:
ggplot(data=df, aes(x=Group, y=Value)) +
geom_bar(stat="identity") +
scale_x_discrete(labels = substring(df$Group, 3, 3), expand=c(0.1,0.1)) +
facet_grid(~ substring(Group, 1, 1), switch="x") +
theme_bw() +
theme(strip.placement = "outside",
strip.background = element_rect(fill=NA,colour="grey50"),
panel.spacing=unit(0,"cm"))
gives:
As you can see, here it gives the "4" correctly. However, when you are setting the labels directly, it is setting them (in order) for each facet separately when you allow the scales to vary. In essence, you are setting the labels to "1, 2, 3, 1, 2, 1, 2, 3, 4" each time, but only using the first few.
If you want to stick closer to your current, you'll also need to set the breaks, e.g.:
ggplot(data=df, aes(x=Group, y=Value)) +
geom_bar(stat="identity") +
scale_x_discrete(breaks = df$Group, labels = substring(df$Group, 3, 3), expand=c(0.1,0.1)) +
facet_grid(~ substring(Group, 1, 1), space="free_x", scales="free_x", switch="x") +
theme_bw() +
theme(strip.placement = "outside",
strip.background = element_rect(fill=NA,colour="grey50"),
panel.spacing=unit(0,"cm"))
Which gives
回答2:
Let's simplify and start in a fresh R session:
df <- structure(list(Group = structure(1:9, .Label = c("1_1", "1_2",
"1_3", "2_1", "2_2", "3_1", "3_2", "3_3", "3_4"), class = "factor"),
Value = c(-1.23, 2.34, 0.56, 1.87, -2.4, 5.54, -0.98, -2.31,
6)), .Names = c("Group", "Value"), row.names = c(NA, -9L), class = "data.frame")
library(ggplot2)
ggplot(data=df, aes(x=Group, y=Value)) +
geom_bar(stat="identity") +
scale_x_discrete(labels = substring(Group, 3, 3), expand=c(0.1,0.1))
# Error in substring(Group, 3, 3) : object 'Group' not found
The scale_x_discrete
arguments don't have access to the within-data environment. (I think there are only two places you can safely reference data column in ggplot2
: inside aes()
and as part of a formula
in a faceting call.) Your code must be finding a Group
object from your global environment, which is probably the whole column. This is why you aren't getting an error in your question, but I am when I use a fresh R session.
Usually the easiest way to make ggplot2
do what you want is to fix your data so that it matches what you want:
df$group_last = substring(df$Group, 3, 3)
ggplot(data=df, aes(x=group_last, y=Value)) +
geom_bar(stat="identity") +
scale_x_discrete(expand=c(0.1,0.1))
At this point you can add your facets and theming back in and everything will work.