【发布时间】:2021-12-30 12:25:23
【问题描述】:
我正在尝试将日志刻度添加到下面条形图的 y 轴。
Bar Plot I am trying to add log ticks to
情节可以很好地使用以下代码,没有 logticks:
plot <- ggplot(data_long, aes(fill= data_long$variable, y= data_long$value, x= data_long$variable)) +
geom_bar(position="dodge", stat="identity", )
plot <- plot + scale_fill_manual("legend", values = c("Casirivimab" = "brown2", "Imdevimab" = "darkorchid1", "Bamlanivimab" = "mediumblue","Etesevimab" = "deeppink", "Sotrovimab" = "cyan3", "Regdanvimab" = "yellow2", "Etesevimab + Bamlanivimab" = "magenta3", "Casirivimab + Imdevimab" = "chartreuse4" ))
plot <- plot + theme(aspect.ratio=1/2, axis.text.x = element_blank(), axis.ticks = element_blank())
plot <- plot + scale_y_continuous(trans='log10', breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))
plot <- plot + annotation_logticks()
plot <- plot + facet_wrap(factor(data_long$Variant, levels = c("B.1.351", "P.1", "B.1.617", "B.1.617.1", "B.1.617.2", "B.1.1.7", "B.1.427", "B.1.429", "B.1.525")), ncol = 9) + labs (x = "", y = "Fold Change Neutralisation", fill = "mAb")
plot <- plot + theme(aspect.ratio=6/2,)
plot <- plot + theme(legend.key.size = unit(0.2, 'cm') )
plot <- plot + theme(panel.grid = element_blank())
plot <- plot + geom_text(aes(label = N), nudge_x = 0.5, nudge_y = 0.25, color="black",vjust = 0.5,hjust = 1.2)
plot
但是添加 annotation_logticks() 函数会产生错误。
plot <- plot + annotation_lockticks()
plot
错误是
$tmp, "PANEL", value = c(1L, 8L, 7L, 6L, : 替换有 41 行,数据有 1
编辑:这是通过 dput() 获得的数据:
structure(list(Variant = c("B.1.351", "B.1.429", "B.1.427", "B.1.1.7",
"B.1.617.2", "B.1.617.1", "B.1.617", "P.1", "B.1.351", "B.1.429",
"B.1.427", "B.1.1.7", "B.1.617.2", "B.1.617.1", "B.1.617", "P.1",
"B.1.351", "B.1.429", "B.1.1.7", "B.1.617.2", "B.1.617.1", "B.1.617",
"P.1", "B.1.351", "B.1.429", "B.1.427", "B.1.1.7", "B.1.617.2",
"B.1.617.1", "B.1.617", "P.1", "B.1.351", "P.1", "B.1.617.2",
"B.1.351", "B.1.617", "B.1.351", "B.1.429", "B.1.1.7", "B.1.617.1",
"B.1.617"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 7L, 7L, 8L, 8L, 8L,
8L, 8L), .Label = c("Casirivimab", "Imdevimab", "Bamlanivimab",
"Etesevimab", "Sotrovimab", "Regdanvimab", "Etesevimab...Bamlanivimab",
"Casirivimab...Imdevimab"), class = "factor"), value = c(862.1,
0.9, 1, 1.4, 1, 17.6, 3.6, 69.6, 1, 5, 0.7, 1.1, 4.2, 3.8, 1,
0.6, 169209.3, 2000, 1.7, 4175.6, 1700, 450, 1814.5, 11140.6,
0.9, 2.1, 5.5, 0.5, 1.2, 0.9, 11915, 0.5, 0.7, 13, 300, 4.7,
1.3, 0.8, 0.6, 0.8, 1), N = c(8L, 2L, 1L, 6L, 4L, 3L, 1L, 4L,
8L, 2L, 1L, 6L, 3L, 3L, 1L, 4L, 6L, 1L, 5L, 3L, 2L, 1L, 3L, 5L,
1L, 1L, 3L, 3L, 2L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L)), row.names = c(NA, -41L), class = "data.frame")
关于如何解决这个问题的任何想法? 谢谢
【问题讨论】:
-
你能用
dput()分享你的数据吗? -
嗨@neuron - 我已通过 dput() 包含数据作为编辑。希望这有帮助吗?感谢您抽出宝贵时间
-
相信我的回答应该能解决你的问题
标签: r ggplot2 logging scale geom-bar