问题
How to highlight the entire bar in which the observations obs.A and obs.B respectively are being allocated using ggplot? The exact same thing has been done for the regular hist() function but what is the ggplot way?
Below some code to illustrate
library(ggplot2)
data <- data.frame(Var=c(rep("A",50),rep("B",50)), Value=round(rnorm(100,10,2),0))
obs.A<-8
obs.B<-10
ggplot(data)+
geom_histogram(aes(x=Value))+
facet_grid(Var ~ .)
Edit: It needs to work for large and small sample sizes and really only highlight one and all of the bar.
回答1:
one ggplot way is to build it into the dataframe used for plotting:
library(ggplot2)
data <- data.frame(Var=c(rep("A",50),rep("B",50)), Value=round(rnorm(100,10,2),0))
obs.A<-8
obs.B<-10
data$color <- ifelse(data$Var == "A" & data$Value == obs.A, T, F)
data$color <- ifelse(data$Var == "B" & data$Value == obs.B, T, data$color)
ggplot(data)+
geom_histogram(aes(x=Value, fill = color))+
facet_grid(Var ~ .)
Note this works easily for your test case because the range for data$Value is 5-16 and the default for geom_histogram() is bins = 30. If you wanted to make it more transferable you would want to set geom_histogram(binwidth = 1) or set data$color based on bins, something like this:
data <- data.frame(Var=c(rep("A",50),rep("B",50)), Value=round(rnorm(100,10,10),0)) # bigger sd
obs.A<-8
obs.B<-10
data$cuts <- cut(data$Value, 30, labels = F)
A_colored_cuts <- unique(data$cuts[data$Value == obs.A])
data$color <- ifelse(data$Var == "A" & data$cuts == A_colored_cuts, T, F)
B_colored_cuts <- unique(data$cuts[data$Value == obs.B])
data$color <- ifelse(data$Var == "B" & data$cuts == B_colored_cuts, T, data$color)
ggplot(data)+
geom_histogram(aes(x=Value, fill = color))+
facet_grid(Var ~ .)
EDIT: For larger sample sizes, we would want to use the second option outlined above and specify geom_histogram(boundary = .5), since we want the bin breaks on integers.
set.seed(1)
data <- data.frame(Var=c(rep("A",50),rep("B",50)), Value=round(rnorm(10000,10,10),0))
#use code chunk 2 above
ggplot(data)+
geom_histogram(aes(x=Value, fill = color), boundary = .5)+
facet_grid(Var ~ .)
回答2:
Adding the conditions inside geom_histogram for the aesthetics fill. We remove the oversized legend with theme(legend.position = "none")
# Example 1
set.seed(12345)
data <- data.frame(Var = c(rep("A", 50), rep("B", 50)), Value = round(rnorm(100, 10, 2), 0))
ggplot(data) +
geom_histogram(aes(x = Value,
fill = Value == 8 & Var == "A" | Value == 10 & Var == "B"), binwidth=0.5) +
facet_grid(Var ~ .) +
theme(legend.position = "none")
# Example 2
set.seed(12345)
data <- data.frame(Var = c(rep("A", 50), rep("B", 50)), Value = round(rnorm(10000, 10, 10), 0))
ggplot(data) +
geom_histogram(aes(x = Value,
fill = Value == 8 & Var == "A" | Value == 10 & Var == "B"), binwidth = 0.5) +
facet_grid(Var ~ .) +
theme(legend.position = "none")
If we would like to assign different colours, we use scale_fill_manual:
# Example3
set.seed(12345)
data <- data.frame(Var = c(rep("A", 50), rep("B", 50)), Value = round(rnorm(100, 10, 2), 0))
ggplot(data) +
geom_histogram(aes(x = Value,
fill = Value == 8 & Var == "A" | Value == 10 & Var == "B"), binwidth=0.5) +
facet_grid(Var ~ .) +
scale_fill_manual(values = c("grey45", "red"))+
theme(legend.position = "none")
回答3:
We can try this to have desired the highlighting for any size of data (we may need to adjust the bindwith with the data size for the bar chart to look prettier and more informative):
library(ggplot2)
set.seed(12345)
data <- data.frame(Var=c(rep("A",50),rep("B",50)), Value=round(rnorm(100,10,2),0))
obs.A<-8
obs.B<-10
cond <- (data$Var=='A' & data$Value == obs.A)|(data$Var=='B' & data$Value == obs.B)
binwidth <- 0.25
ggplot(data)+
geom_histogram(data=data[!cond,], aes(x=Value), binwidth=binwidth) +
geom_histogram(data=data[cond,], aes(x=Value), fill='red', binwidth=binwidth) +
facet_grid(Var ~ .)
set.seed(12345)
data <- data.frame(Var=c(rep("A",50),rep("B",50)), Value=round(rnorm(10000,10,10),0))
obs.A<-8
obs.B<-10
cond <- (data$Var=='A' & data$Value == obs.A)|(data$Var=='B' & data$Value == obs.B)
binwidth <- 0.5
ggplot(data)+
geom_histogram(data=data[!cond,], aes(x=Value), binwidth=binwidth) +
geom_histogram(data=data[cond,], aes(x=Value), fill='red', binwidth=binwidth) +
facet_grid(Var ~ .)
来源:https://stackoverflow.com/questions/41792233/how-to-highlight-bin-of-observation-in-ggplot