【问题标题】:Using variable elements as superscripts and subscripts for Plot labels on ggplot2使用可变元素作为 ggplot2 上的绘图标签的上标和下标
【发布时间】:2018-09-22 05:51:09
【问题描述】:

使用geom_jitter(),我绘制ab。对于绘图上的每个点,我想显示形式为 $c^(d)_(e)$ 的标签,即带有上标 d 和下标 e 的变量 c,其中 c、d 和 e 是与 a 关联的对应值和数据中的 b。

我尝试将geom_text()bquote(.(c)^d[e]) 一起使用,但它显示错误:

Don't know how to automatically pick scale for object of type call. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (42): label, x, y

您可以在下面找到我正在使用的代码和数据。

a <- c(1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)

b <- c(9.75, 19.0, 9.75, 4.02, 3.1, 19.0, 9.27, 17.2, 9.27, 19.0, 7.78, 6.06, 9.75, 
       4.02, 3.05, 3.1, 2.59, 2.29, 19.0, 9.27, 3.93, 17.2, 3.05, 15.59, 9.27, 
       3.93, 3.05, 19.0, 9.27, 7.47, 17.2, 7.47, 9.27, 5.87, 5.87, 8.82, 19.0, 
       7.78, 5.87, 6.06, 5.01, 4.45)

c <- c('(0, 1)', '(1, 0)', '(0, 2)', '(0, 2)', '(0, 2)', '(1, 1)', '(1, 1)', '(1, 1)', 
       '(1, 1)', '(2, 0)', '(2, 0)', '(2, 0)', '(0, 3)', '(0, 3)', '(0, 3)', '(0, 3)', 
       '(0, 3)', '(0, 3)', '(1, 2)', '(1, 2)', '(1, 2)', '(1, 2)', '(1, 2)', '(1, 2)', 
       '(1, 2)', '(1, 2)', '(1, 2)', '(2, 1)', '(2, 1)', '(2, 1)', '(2, 1)', '(2, 1)', 
       '(2, 1)', '(2, 1)', '(2, 1)', '(2, 1)', '(3, 0)', '(3, 0)', '(3, 0)', '(3, 0)', 
       '(3, 0)', '(3, 0)')

d <- c('(0, 0)', '(0, 0)', '(0, 0)', '(0, 0)', '(0, 1)', '(0, 0)', '(0, 0)', '(0, 1)', 
       '(1, 0)', '(0, 0)', '(0, 0)', '(1, 0)', '(0, 0)', '(0, 0)', '(0, 0)', '(0, 1)', 
       '(0, 1)', '(0, 2)', '(0, 0)', '(0, 0)', '(0, 0)', '(0, 1)', '(0, 1)', '(0, 2)', 
       '(1, 0)', '(1, 0)', '(1, 1)', '(0, 0)', '(0, 0)', '(0, 0)', '(0, 1)', '(0, 1)', 
       '(1, 0)', '(1, 0)', '(1, 1)', '(2, 0)', '(0, 0)', '(0, 0)', '(0, 0)', '(1, 0)', 
       '(1, 0)', '(2, 0)')

e <- c('(0, 1)', '(1, 0)', '(0, 1)', '(0, 2)', '(0, 2)', '(1, 0)', '(1, 1)', '(1, 1)', 
       '(1, 1)', '(1, 0)', '(2, 0)', '(2, 0)', '(0, 1)', '(0, 2)', '(0, 3)', '(0, 2)', 
       '(0, 3)', '(0, 3)', '(1, 0)', '(1, 1)', '(1, 2)', '(1, 1)', '(1, 2)', '(1, 2)', 
       '(1, 1)', '(1, 2)', '(1, 2)', '(1, 0)', '(1, 1)', '(2, 1)', '(1, 1)', '(2, 1)', 
       '(1, 1)', '(2, 1)', '(2, 1)', '(2, 1)', '(1, 0)', '(2, 0)', '(3, 0)', '(2, 0)', 
       '(3, 0)', '(3, 0)')

data <- data.frame(a, b, c, d, e)

library(ggplot2)
c <- ggplot(data, aes(a, b)) + 
  geom_jitter(width = 0.2) + 
  geom_text(aes(label = bquote(.(c)^.(d)[.(e)])), 
            position = position_jitter(width = 0.2, height = 0))

【问题讨论】:

    标签: r ggplot2 label annotate


    【解决方案1】:

    试试这个,

    dd <- data.frame(a, b, c, d, e, stringsAsFactors = FALSE)
    dd$lab <- apply(dd, 1, function(row) bquote(.(row[1])^.(row[2])[.(row[3])]))
    
    library(ggplot2)
    ggplot(dd, aes(a, b)) + 
      geom_jitter(width = 0.2) + 
      geom_text(aes(label = lab), parse = TRUE,
                position = position_jitter(width = 0.2, height = 0))
    

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 2015-02-11
      • 2022-01-18
      • 1970-01-01
      • 2021-08-11
      • 1970-01-01
      • 2016-02-05
      • 2020-12-18
      • 2016-04-25
      相关资源
      最近更新 更多