Annotation_logticks() 错误。 `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 8L, 7L, 6L,

Posted

技术标签:

【中文标题】Annotation_logticks() 错误。 `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 8L, 7L, 6L, : 替换有 41 行,数据有 1【英文标题】:Annotation_logticks() Error. Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 8L, 7L, 6L, : replacement has 41 rows, data has 1 【发布时间】: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() 包含数据作为编辑。希望这有帮助吗?感谢您抽出宝贵时间 相信我的回答应该能解决你的问题 【参考方案1】:

您已接近您在问题中提供的代码。订购图表并在图表侧面添加对数刻度的最简单方法是,您需要在数据框中考虑 Variant 列。然后,您可以将facet_grid 应用于分解列和对数刻度。

data_long$Variant_f <- 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"))

plot <- ggplot(data_long, aes(fill= data_long$variable, y= data_long$value, x= data_long$variable)) + 
  geom_bar(position="dodge", stat="identity") + 
  scale_fill_manual("legend", values = c("Casirivimab" = "brown2", "Imdevimab" = "darkorchid1", "Bamlanivimab" = "mediumblue","Etesevimab" = "deeppink", "Sotrovimab" = "cyan3", "Regdanvimab" = "yellow2", "Etesevimab + Bamlanivimab" = "magenta3", "Casirivimab + Imdevimab" = "chartreuse4")) +
  theme(aspect.ratio=1/2, axis.text.x = element_blank(), axis.ticks = element_blank()) + 
  scale_y_continuous(trans='log10', breaks = trans_breaks("log10", function(x) 10^x),
                                  labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides = "l") +
  facet_grid(col = vars(Variant_f)) +
  geom_text(aes(label = N), nudge_x = 0.5, nudge_y = 0.25, color="black",vjust = 0.5,hjust = 1.2) +
  theme(aspect.ratio=6/2,
        legend.key.size = unit(0.2, 'cm'),
        panel.grid = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

图表中有一些美学需要修复,但它可以实现您的目标。

如果您只想将对数刻度应用于其中一个图表,您可以使用函数。请注意,这是可选的。但是,我认为它看起来更好。

将刻度添加到一个图表的功能:

add_logticks  <- function (base = 10, sides = "bl", scaled = TRUE, 
                           short = unit(0.1, "cm"), mid = unit(0.2, "cm"),  long = unit(0.3, "cm"), 
                           colour = "black",  size = 0.5, linetype = 1, alpha = 1, color = NULL, 
                           data =data.frame(x = NA),... )   
  if (!is.null(color)) 
    colour <- color
  layer(geom = "logticks", params = list(base = base, 
                                              sides = sides, scaled = scaled, short = short, 
                                              mid = mid, long = long, colour = colour, size = size, 
                                              linetype = linetype, alpha = alpha, ...), 
        stat = "identity", data = data , mapping = NULL, inherit.aes = FALSE, position = "identity",
        show.legend = FALSE)

然后剧情:

plot <- ggplot(data_long, aes(fill= data_long$variable, y= data_long$value, x= data_long$variable)) + 
  geom_bar(position="dodge", stat="identity") + 
  scale_fill_manual("legend", values = c("Casirivimab" = "brown2", "Imdevimab" = "darkorchid1", "Bamlanivimab" = "mediumblue","Etesevimab" = "deeppink", "Sotrovimab" = "cyan3", "Regdanvimab" = "yellow2", "Etesevimab + Bamlanivimab" = "magenta3", "Casirivimab + Imdevimab" = "chartreuse4")) +
  theme(aspect.ratio=1/2, axis.text.x = element_blank(), axis.ticks = element_blank()) + 
  scale_y_continuous(trans='log10', breaks = trans_breaks("log10", function(x) 10^x),
                                  labels = trans_format("log10", math_format(10^.x))) +
  #annotation_logticks(sides = "l") +
  facet_grid(col = vars(Variant_f)) +
  geom_text(aes(label = N), nudge_x = 0.5, nudge_y = 0.25, color="black",vjust = 0.5,hjust = 1.2) +
  theme(aspect.ratio=6/2,
        legend.key.size = unit(0.2, 'cm'),
        panel.grid = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

然后应用于情节:

plot + add_logticks(side = 'l', data = data.frame(x= NA, Variant_f = 'B.1.351'))

【讨论】:

太好了,谢谢。 不过有一个小问题。您的方法重新排序网格方面(B.1.1.7 在您的方法中排在第一位,但 B.1.351 在我的排位中排在第一位)。这个很重要。通过重新排序原始数据集中的变体,我能够在使用你方法的第一部分时解决这个问题。这给出了一个具有正确顺序的刻面的图,所有刻面的逻辑标记。使用 logticks 函数成功地仅在其中一个图/方面放置了刻度,但它再次混淆了顺序然后出现。有什么想法吗? @M.J.Cox 我根据您上面的评论更新了我的答案

以上是关于Annotation_logticks() 错误。 `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 8L, 7L, 6L, 的主要内容,如果未能解决你的问题,请参考以下文章

Pig 安装错误:错误 pig.Main:错误 2998:未处理的内部错误

Informix 错误:发生了语法错误。错误代码:-201

我收到一个错误:“MetaMask - RPC 错误:错误:错误:[ethjs-rpc] rpc 错误与有效负载”

错误精灵错误跟踪器错误

网页打开显示错误500是啥意思

PHP错误处理