如何使用ggplot2以不同方式在此负片和正片中添加轴文本?

时间:2017-03-07 08:08:21

标签: r ggplot2

我已经绘制了带有负条形和正条形的条形图,research对此很熟悉。但是,我的代码使用horizontalScrollViewB.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { horizontalScrollViewD.scrollTo(horizontalScrollViewB.getScrollX(), horizontalScrollViewB.getScrollY()); } }); horizontalScrollViewD.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { horizontalScrollViewB.scrollTo(horizontalScrollViewD.getScrollX(), horizontalScrollViewD.getScrollY()); } }); graphics::plot()非常不方便且冗长,如下所示。尽我所能,我可以使用graphics::text()element_text中找到解决方案。请帮助或尝试在ggplot2中提供一些如何实现此目的的建议。提前致谢 enter image description here

ggplot2

3 个答案:

答案 0 :(得分:6)

这是一个解决方案,使用dplyr为标签位置和对齐创建一些额外的列,然后将图表合理地匹配您原来拥有的内容:

library("dplyr")
library("ggplot2")
df <- df %>%
  mutate(
    genus = factor(genus, levels = genus[order(value, decreasing = TRUE)]),
    label_y = ifelse(value < 0, 0.2, -0.2),
    label_hjust = ifelse(value < 0, 0, 1)
  )

my_plot <- ggplot(df, aes(x = genus, y = value, fill = class)) +
  geom_bar(stat = "identity", col = "black") +
  geom_text(aes(y = label_y, label = genus, hjust = label_hjust)) +
  coord_flip() +
  scale_fill_manual(values = c(groupA = "forestgreen", groupB = "goldenrod")) +
  theme_minimal() +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "top",
        legend.justification = 0.05,
        legend.title = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x = element_line(colour = "grey80", linetype = "dashed"),
        panel.grid.minor.x = element_blank()) +
  scale_y_continuous(expression(log[10](italic("LDA score"))),
                     breaks = -6:6, limits = c(-6, 6))

print(my_plot)
ggsave("lefse.tiff", width = 5, height = 5, dpi = 400, my_plot)

enter image description here

答案 1 :(得分:5)

我会试试这个:

library(ggplot2)

# change the factor levels so it will be displayed in correct order
df$genus <- factor(df$genus, levels = as.character(df$genus))

ggplot(df, aes(x = genus, y = value)) +
    geom_bar(aes(fill = class), stat = 'identity') +  # color by class
    coord_flip() +  # horizontal bars
    geom_text(aes(y = 0, label = genus, hjust = as.numeric(value > 0))) +  # label text based on value
    theme(axis.text.y = element_blank())

enter image description here

在上面,hjust将改变文本相对于其y位置的方向(现在翻转为x),这类似于基础R图中的pos参数。因此,您也可以使用pos函数的text参数向量简化代码。

答案 2 :(得分:1)

两个选项:

"line 1:0 mismatched input 'foo' expecting 'hello'"

enter image description here

或者这个:

library(ggplot2)

# my data
df <- data.frame(genus=c("Prevotella","Streptococcus","YRC22","Phascolarctobacterium","SMB53","Epulopiscium",
                         "CF231","Anaerovibrio","Paludibacter","Parabacteroides","Desulfovibrio","Sutterella",
                         "Roseburia","Others__0_5_","Akkermansia","Bifidobacterium","Campylobacter","Fibrobacter",
                         "Coprobacillus","Bulleidia","f_02d06","Dorea","Blautia","Enterococcus","Eubacterium",
                         "p_75_a5","Clostridium","Coprococcus","Oscillospira","Escherichia","Lactobacillus"),
                 class=c(rep("groupA",18),rep("groupB",13)),
                 value=c(4.497311,4.082377,3.578472,3.567310,3.410453,3.390026,
                         3.363542,3.354532,3.335634,3.284165,3.280838,3.218053,
                         3.071454,3.026663,3.021749,3.004152,2.917656,2.811455,
                         -2.997631,-3.074314,-3.117659,-3.151276,-3.170631,-3.194323,
                         -3.225207,-3.274281,-3.299712,-3.299875,-3.689051,-3.692055,
                         -4.733154)
)

ggplot(df, aes(reorder(genus, -value), value, fill = class)) +
    geom_bar(stat = "identity") +
    coord_flip() +
    geom_text(aes(label = genus,
                  y = ifelse(value < 1, 1.5, -1.5)), size = 2.5) +
    theme(axis.title.y=element_blank(),
          axis.text.y=element_blank(),
          axis.ticks.y=element_blank())

enter image description here