我使用以下内容绘制下面给出的图表:
data <- structure(list(Type1 = c("DB", "DB", "DB", "DB", "DB", "DB",
"DB", "DB", "DB", "DB", "DB", "DB", "DB", "Manual", "Manual",
"Manual", "Manual", "Manual", "Manual", "Manual", "Manual", "Manual",
"Manual", "Manual", "Manual", "Manual", "Manual", "Manual"),
Type2 = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L), Category = c("A", "B", "C", "D", "E", "A", "B", "F",
"G", "A", "B", "C", "H", "I", "J", "K", "L", "M", "O", "J",
"P", "K", "Q", "M", "K", "P", "J", "P"), Percent = c("83.5106383",
"9.574468085", "5.85106383", "0.531914894", "1/188*100",
"85.24590164", "11.47540984", "1/61*100", "1.639344262",
"90", "3.333333333", "3.333333333", "3.333333333", "20.10582011",
"10.05291005", "6.349206349", "5.82010582", "4.761904762",
"31.14754098", "16.39344262", "6.557377049", "6.557377049",
"4.918032787", "30", "23.33333333", "16.66666667", "10",
"6.666666667")), .Names = c("Type1", "Type2", "Category",
"Percent"), row.names = c(NA, -28L), class = "data.frame")
data$Percent <- as.numeric(data$Percent)
g= ggplot(data, aes(x=Category, y=Percent)) +
geom_bar(width=0.8, stat="identity", position=position_dodge()) +
facet_grid(Type1 ~ Type2, ) +
theme_bw() +
coord_flip() +
scale_y_continuous(limits=c(0,100))
print(g)
有什么方法可以让我的情节如此,每行只使用非零的标签?例如,取DB
行。它仅使用6个标签,但显示所有16个标签,因为其他10个标签正由Manual
部分使用。同样适用于Manual
行。我正在寻找的是这样的:
1 2 3
H
G
D DB
C
B
A
Q
P
O
G MANUAL
M
L
K
J
I
有关如何执行此操作的任何建议吗?
答案 0 :(得分:3)
它只会删除那些不在任何方面的元素。但是你可以在facet_grid()中使用scales =“free”参数。
g + facet_grid(Type1 ~ Type2, scales="free")
但我认为facet_wrap可能会为您提供更合适的可视化:
ggplot(data, aes(x=Category, y=Percent)) +
geom_bar(width=0.8, stat="identity", position=position_dodge()) +
facet_wrap(Type1 ~ Type2,scales="free") +
theme_bw() +
scale_y_continuous(c(0,100)) +
coord_flip() +
opts()
答案 1 :(得分:2)
合并facet_grid(scales="free")
和coord_flip()
时似乎存在错误或意外行为。
以下是两种可能的解决方法:
library(ggplot2)
# Removed quotes from Percent values, so that Percent will be numeric.
dat <- structure(list(Type1 = c("DB", "DB", "DB", "DB", "DB", "DB",
"DB", "DB", "DB", "DB", "DB", "DB", "DB", "Manual", "Manual",
"Manual", "Manual", "Manual", "Manual", "Manual", "Manual", "Manual",
"Manual", "Manual", "Manual", "Manual", "Manual", "Manual"),
Type2 = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L), Category = c("A", "B", "C", "D", "E", "A", "B", "F",
"G", "A", "B", "C", "H", "I", "J", "K", "L", "M", "O", "J",
"P", "K", "Q", "M", "K", "P", "J", "P"), Percent = c(83.5106383,
9.574468085, 5.85106383, 0.531914894, 1/188*100,
85.24590164, 11.47540984, 1/61*100, 1.639344262,
90, 3.333333333, 3.333333333, 3.333333333, 20.10582011,
10.05291005, 6.349206349, 5.82010582, 4.761904762,
31.14754098, 16.39344262, 6.557377049, 6.557377049,
4.918032787, 30, 23.33333333, 16.66666667, 10,
6.666666667)), .Names = c("Type1", "Type2", "Category",
"Percent"), row.names = c(NA, -28L), class = "data.frame")
figure_1 = ggplot(dat, aes(x=Category, y=Percent)) +
geom_bar(width=0.8, stat="identity") +
facet_grid(Type2 ~ Type1, scales="free_x") +
theme_bw() +
scale_y_continuous(limits=c(0, 100)) +
opts(title="Figure 1. Success!\n(But Rotated 90 Degrees)")
figure_2 = ggplot(dat, aes(x=Percent, y=Category)) +
geom_point(size=3) +
facet_grid(Type1 ~ Type2, scales="free_y") +
theme_bw() +
scale_x_continuous(limits=c(0, 100)) +
opts(title="Figure 2. Success!\n(But Dotplot Instead Of Barplot)")
# Unexpected interaction between scales="free" and coord_flip()?
figure_3 = ggplot(dat, aes(x=Category, y=Percent)) +
geom_bar(width=0.8, stat="identity") +
facet_grid(Type1 ~ Type2, scales="free") +
theme_bw() +
scale_y_continuous(limits=c(0, 100)) +
coord_flip() +
opts(title="Figure 3. Strange Y-axis Behavior.")