简体   繁体   中英

Annotation_logticks() Error. Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 8L, 7L, 6L, : replacement has 41 rows, data has 1

I am trying to add log ticks to the y axis of the bar plot below.

Bar Plot I am trying to add log ticks to

The plot works fine with the following code, without 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 

However adding the annotation_logticks() function creates an error.

    plot <- plot + annotation_lockticks()  
    plot  

The error is

Error in $<-.data.frame( tmp , "PANEL", value = c(1L, 8L, 7L, 6L, : replacement has 41 rows, data has 1

EDIT: here is the data via 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")

any ideas on how to get around this? Thanks

You were close to the code you provided in your question. The main thing that need to change was what your were specifying in facet_grid or facet_wrap . I used facet_grid because it matched your picture better. You just need to use vars on your data in facet_grid() . See the code below

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(cols = vars(Variant)) +
  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))

There are some aesthetics that need to be fixed in the graph but it achieves your goal.

例子

If you wanted to get fancy, you could add the log ticks to only one of the plots using the following code

Function to add the ticks to one graph:

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)
}

Then the plot:

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(cols = vars(Variant)) +
  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))

Then apply to the plot:

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

示例2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM