具有独立轴R的背对背条形图
我想画背靠背的条形图,但是每一边都在一个独立的轴上。我可以通过取一个集合的负数来背靠背地绘制它们,但这会使它们处于相同的访问权限,并且因为pvalues较小,所以它们的条形图几乎无法表示具有独立轴R的背对背条形图,r,ggplot2,R,Ggplot2,我想画背靠背的条形图,但是每一边都在一个独立的轴上。我可以通过取一个集合的负数来背靠背地绘制它们,但这会使它们处于相同的访问权限,并且因为pvalues较小,所以它们的条形图几乎无法表示 library(ggplot2) df <-structure(list(Description = c("a", "b", "c", "d", "e", "f", "g", "h", "a", "b", "c", "d", "e", "f", "g", "h"), test = c("size
library(ggplot2)
df <-structure(list(Description = c("a", "b", "c", "d", "e", "f",
"g", "h", "a", "b", "c", "d", "e", "f", "g", "h"), test = c("size",
"size", "size", "size", "size", "size", "size", "size", "p",
"p", "p", "p", "p", "p", "p", "p"), value = c(0.1, 0.1, 0.125,
0.1, 0.075, 0.1, 0.075, 0.125, 0.000230705311441713, 0.000314488619269942,
0.00106639822095382, 0.00108290238851994, 0.00114723539549198,
0.00160204850890075, 0.0019276388745184, 0.00320371567547557)), .Names = c("Description",
"test", "value"), row.names = c(NA, -16L), class = "data.frame")
df$value[df$test == 'p'] <- -(df$value[df$test == 'p'])
ggplot(df, aes(x=Description, y= value, group=test, fill=test)) + geom_col() +coord_flip()
库(ggplot2)
df您可以通过使用镶嵌面和调整来删除镶嵌面之间的间距来执行此操作:
ggplot(df, aes(x=Description, y= value, fill=test)) +
facet_wrap(~ test, scales = "free_x") +
geom_col() +
coord_flip() +
scale_y_continuous(expand = c(0, 0)) +
theme(panel.spacing.x = unit(0, "mm"))
这可能会在轴标签上产生一些问题,这些问题的解决可能有点棘手。在这种情况下,在两个面之间保持一些空间可能会更容易,但不必在中间遇到杆。
输出:
PS:您还可以使用以下方式删除负轴标签:
scale_y_continuous(
expand = c(0, 0),
labels = function(x) signif(abs(x), 3)
)
@Marius解决方案比此解决方案更简单,但这允许独立地对每个图形进行更多控制
我必须删除p1右侧和p2左侧的绘图页边距。由于某些原因,需要在左边空白处添加填充-3.5pt以使其齐平,不确定这是否会在所有绘图中保持一致。另一个手动操作是更改一个轴上的打断,以便不在另一个轴上绘制0
我也不需要负p值,只要使用scale\u y\u reverse
p1 <- ggplot(df[df$test == 'p',], aes(x=Description, y= value)) + geom_col(fill='red') + theme_minimal()+
coord_flip() + scale_y_reverse(name= "axis1",expand = expand_scale(mult= c(c(0.05,0)))) +
theme(panel.spacing.x = unit(0, "mm")) +theme(plot.margin = unit(c(5.5, 0, 5.5, 5.5), "pt"))
p2 <- ggplot(df[df$test != 'p',], aes(x=Description, y= value)) + geom_col(fill='blue') +
scale_y_continuous(name = "axis2", breaks = seq(0.025, 0.125, 0.025) ,expand = expand_scale(mult= c(c(0,0.05)))) +
coord_flip() +
theme(panel.spacing.x = unit(0, "mm"))+ theme_minimal() +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(),
axis.line.y = element_blank(), axis.ticks.y=element_blank(),
plot.margin = unit(c(5.5, 5.5, 5.5, -3.5), "pt"))
grid.newpage()
grid.draw(cbind(ggplotGrob(p1), ggplotGrob(p2), size = "last"))
p1我已经适应了我的需要。张凌云获得了荣誉
library(dplyr)
library(ggplot2)
set.seed(123)
ten_positive_rand_numbers <- abs(rnorm(10)) + 0.1
the_prob <- ten_positive_rand_numbers / sum(ten_positive_rand_numbers)
fk_data <- data.frame(job_type = sample(LETTERS[1:10], 1000,
replace = TRUE, prob = the_prob),
gender = sample(c("Male", "Female"), 1000,
replace = TRUE))
# prepare data for plotting
plotting_df <-
fk_data %>%
group_by(job_type, gender) %>%
summarise(Freq = n()) %>%
# a trick!
mutate(Freq = if_else(gender == "Male", -Freq, Freq))
## find the order
temp_df <-
plotting_df %>%
filter(gender == "Female") %>%
arrange(Freq)
the_order <- temp_df$job_type
# plot
p <-
plotting_df %>%
ggplot(aes(x = job_type, y = Freq, group = gender, fill = gender)) +
geom_bar(stat = "identity", width = 0.75) +
coord_flip() +
scale_x_discrete(limits = the_order) +
# another trick!
scale_y_continuous(breaks = seq(-150, 150, 50),
labels = abs(seq(-150, 150, 50))) +
labs(x = "Job type", y = "Count", title = "Back-to-back bar chart") +
theme(legend.position = "bottom",
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "grey90")) +
# reverse the order of items in legend
# guides(fill = guide_legend(reverse = TRUE)) +
# change the default colors of bars
scale_fill_manual(values = c("red", "blue"),
name = "",
breaks = c("Male", "Female"),
labels = c("Male", "Female"))
print(p)
库(dplyr)
图书馆(GG2)
种子集(123)
十个正随机数%
安排(频率)
我会考虑这是否是表示数据的好方法。如果轴是独立的,那么绘制值时是否应将其视为可比较的?也许p与大小的散点图是传达信息的更好方式?我认真地考虑到,大小是对我的数据的一个糟糕的描述。它基本上是超几何测试的结果,A-H是亚组,在这种情况下是过度代表的,大小是在我的数据中找到的匹配每个亚组的项目与每个亚组的总数的比率。这有意义吗?我认为这两个图将很好地显示项目的重要性和比例。这是一个很好的解决方案。唯一的改进是绘图窗口是否比条形图稍大。我知道您可以使用expand_scale
独立调整ggplot v3中的ylim,但由于必须反转一个绘图,因此两个绘图的下限和上限都反转了。不过,我很乐意接受你的回答,谢谢。是的,这很棘手,我认为你需要在每个方面设定不同的限制,这是我无法想象的。