r 抖动堆积条形图
r jitter stacked bar plot
我正在绘制堆积箱线图来表示下面的数据
Choice Race Freq countT per
<fct> <ord> <dbl> <dbl> <chr>
1 Pepsi Asian 362 20684 1.7501%
2 Coke Asian 363 20684 1.755%
3 Sprite Asian 204 20684 0.9863%
4 Gatorade Asian 1 20684 0.0048%
5 Pepsi Black 150 20684 0.7252%
6 Coke Black 408 20684 1.9725%
7 Sprite Black 322 20684 1.5568%
8 Gatorade Black 45 20684 0.2176%
9 Pepsi Other 1088 20684 5.2601%
10 Coke Other 2407 20684 11.637%
11 Sprite Other 223 20684 1.0781%
12 Gatorade Other 113 20684 0.5463%
13 Pepsi White 2384 20684 11.5258%
14 Coke White 5038 20684 24.357%
15 Sprite White 6882 20684 33.2721%
16 Gatorade White 694 20684 3.3553%
下面是我的代码
library(ggplot2)
colortest <- c("#FBCF357F", "#ED4C1C7F", "#9C7E707F", "#5AC2F17F" )
ggplot(testdf, aes(x = Choice, y = Freq, fill = Race, label = Freq)) +
geom_bar(stat = "identity") +
geom_text(size = 4, position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = colortest) +
scale_colour_manual(values = colortest) +
coord_flip() +
theme_bw() +
ylab("Count by Race")
我看到的是一系列重叠的标签,像这样通常难以阅读。
我怎样才能偏移这些重叠标签的位置,以便我看到没有重叠的数字?
我尝试了许多其他抖动选项,但都失败了。
ggrepel
轻松俗气。 :-)
代码
library(ggplot2)
library(ggrepel)
colortest <- c("#FBCF357F", "#ED4C1C7F", "#9C7E707F", "#5AC2F17F" )
ggplot(df, aes(x = Choice, y = Freq, fill = Race, label = Freq)) +
geom_bar(stat = "identity") +
# Here it is
geom_text_repel(size = 4, position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = colortest) +
scale_colour_manual(values = colortest) +
coord_flip() +
theme_bw() +
ylab("Count by Race")
数据
df <- read.table(text = "Choice Race Freq countT per
Pepsi Asian 362 20684 1.7501%
Coke Asian 363 20684 1.755%
Sprite Asian 204 20684 0.9863%
Gatorade Asian 1 20684 0.0048%
Pepsi Black 150 20684 0.7252%
Coke Black 408 20684 1.9725%
Sprite Black 322 20684 1.5568%
Gatorade Black 45 20684 0.2176%
Pepsi Other 1088 20684 5.2601%
Coke Other 2407 20684 11.637%
Sprite Other 223 20684 1.0781%
Gatorade Other 113 20684 0.5463%
Pepsi White 2384 20684 11.5258%
Coke White 5038 20684 24.357%
Sprite White 6882 20684 33.2721%
Gatorade White 694 20684 3.3553%", strip.white = TRUE, header = TRUE)
我正在绘制堆积箱线图来表示下面的数据
Choice Race Freq countT per
<fct> <ord> <dbl> <dbl> <chr>
1 Pepsi Asian 362 20684 1.7501%
2 Coke Asian 363 20684 1.755%
3 Sprite Asian 204 20684 0.9863%
4 Gatorade Asian 1 20684 0.0048%
5 Pepsi Black 150 20684 0.7252%
6 Coke Black 408 20684 1.9725%
7 Sprite Black 322 20684 1.5568%
8 Gatorade Black 45 20684 0.2176%
9 Pepsi Other 1088 20684 5.2601%
10 Coke Other 2407 20684 11.637%
11 Sprite Other 223 20684 1.0781%
12 Gatorade Other 113 20684 0.5463%
13 Pepsi White 2384 20684 11.5258%
14 Coke White 5038 20684 24.357%
15 Sprite White 6882 20684 33.2721%
16 Gatorade White 694 20684 3.3553%
下面是我的代码
library(ggplot2)
colortest <- c("#FBCF357F", "#ED4C1C7F", "#9C7E707F", "#5AC2F17F" )
ggplot(testdf, aes(x = Choice, y = Freq, fill = Race, label = Freq)) +
geom_bar(stat = "identity") +
geom_text(size = 4, position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = colortest) +
scale_colour_manual(values = colortest) +
coord_flip() +
theme_bw() +
ylab("Count by Race")
我看到的是一系列重叠的标签,像这样通常难以阅读。
我怎样才能偏移这些重叠标签的位置,以便我看到没有重叠的数字?
我尝试了许多其他抖动选项,但都失败了。
ggrepel
轻松俗气。 :-)
代码
library(ggplot2)
library(ggrepel)
colortest <- c("#FBCF357F", "#ED4C1C7F", "#9C7E707F", "#5AC2F17F" )
ggplot(df, aes(x = Choice, y = Freq, fill = Race, label = Freq)) +
geom_bar(stat = "identity") +
# Here it is
geom_text_repel(size = 4, position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = colortest) +
scale_colour_manual(values = colortest) +
coord_flip() +
theme_bw() +
ylab("Count by Race")
数据
df <- read.table(text = "Choice Race Freq countT per
Pepsi Asian 362 20684 1.7501%
Coke Asian 363 20684 1.755%
Sprite Asian 204 20684 0.9863%
Gatorade Asian 1 20684 0.0048%
Pepsi Black 150 20684 0.7252%
Coke Black 408 20684 1.9725%
Sprite Black 322 20684 1.5568%
Gatorade Black 45 20684 0.2176%
Pepsi Other 1088 20684 5.2601%
Coke Other 2407 20684 11.637%
Sprite Other 223 20684 1.0781%
Gatorade Other 113 20684 0.5463%
Pepsi White 2384 20684 11.5258%
Coke White 5038 20684 24.357%
Sprite White 6882 20684 33.2721%
Gatorade White 694 20684 3.3553%", strip.white = TRUE, header = TRUE)