【问题标题】:How to increase aesthetic qualities of ggplot?如何增加ggplot的审美品质?
【发布时间】:2020-11-24 11:36:01
【问题描述】:

我目前正在处理一些绘制的数据,并设法包含一个图例并用不同的颜色使y 轴多样化。鉴于这一切,我希望其中一个图 PP 是一条虚线,并在峰值点上包含一个黑点,在此点旁边有一条延伸到文本 MPP 的线,以及从此延伸的线像这样指向xy 轴:

到目前为止,这是我的代码:

library(tidyverse)

exp <- read.csv("experimental-IVdata-Lab-3-3.csv")
exp$PP <- exp$current * exp$voltage
ggplot(exp, aes(voltage)) + geom_line(aes(y = current , colour = "current"))  + geom_line(aes(y=PP , colour= "PP"), linetype= 2) + scale_colour_manual(values=c("red", "green"))

可重现的例子:

structure(list(current = c(378, 376, 376, 376, 376, 376, 376, 
376, 376, 376, 376, 376, 376, 375, 375, 375, 375, 375, 375, 375, 
375, 375, 375, 375, 375, 375, 374, 372, 371, 363, 351, 336, 321, 
305, 291, 266, 244, 225, 208, 151, 118, 83, 64, 52, 43, 37, 33, 
29, 26, 18, 13, 9, 7, 5, 5, 4, 3, 3, 2, 1, 0.9, 0.7, 0.5, 0.5, 
0.4, 0.3, 0.3, 0.1, 0.1, 0.05, 0.02, 0.01, 0), voltage = c(0, 
0.505, 0.506, 0.509, 0.512, 0.516, 0.519, 0.522, 0.523, 0.527, 
0.528, 0.53, 0.562, 0.589, 0.616, 0.643, 0.669, 0.698, 0.729, 
0.753, 0.781, 0.838, 0.918, 1, 1.05, 1.134, 1.209, 1.263, 1.303, 
1.41, 1.487, 1.548, 1.592, 1.63, 1.66, 1.705, 1.741, 1.769, 1.791, 
1.86, 1.895, 1.93, 1.95, 1.96, 1.97, 1.972, 1.976, 1.979, 1.981, 
1.988, 1.992, 1.995, 1.997, 1.998, 1.998, 1.999, 1.999, 1.999, 
1.999, 1.999, 1.999, 1.999, 1.999, 1.999, 1.999, 1.999, 1.999, 
1.999, 1.999, 1.999, 2, 2, 2.002), PP = c(0, 189.88, 190.256, 
191.384, 192.512, 194.016, 195.144, 196.272, 196.648, 198.152, 
198.528, 199.28, 211.312, 220.875, 231, 241.125, 250.875, 261.75, 
273.375, 282.375, 292.875, 314.25, 344.25, 375, 393.75, 425.25, 
452.166, 469.836, 483.413, 511.83, 521.937, 520.128, 511.032, 
497.15, 483.06, 453.53, 424.804, 398.025, 372.528, 280.86, 223.61, 
160.19, 124.8, 101.92, 84.71, 72.964, 65.208, 57.391, 51.506, 
35.784, 25.896, 17.955, 13.979, 9.99, 9.99, 7.996, 5.997, 5.997, 
3.998, 1.999, 1.7991, 1.3993, 0.9995, 0.9995, 0.7996, 0.5997, 
0.5997, 0.1999, 0.1999, 0.09995, 0.04, 0.02, 0)), row.names = c(NA, 
-73L), class = c("tbl_df", "tbl", "data.frame"))

更新
我已经设法添加了虚线。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以在坐标轴和PP 的最大值之间绘制geom_segments。 PP的最大y坐标可以用max(exp$PP)找到,x坐标在exp$voltage[which.max(exp$PP)]

    ggplot(exp, aes(voltage)) + 
      geom_line(aes(y = current , colour = "current"))  + 
      geom_line(aes(y=PP , colour= "PP"), linetype= 2) + 
      geom_segment(x = exp$voltage[which.max(exp$PP)], 
                   xend = exp$voltage[which.max(exp$PP)],
                   y = 0, yend = max(exp$PP)) +
      geom_segment(x = 0, xend = exp$voltage[which.max(exp$PP)],
                 y = max(exp$PP), yend = max(exp$PP)) +
      geom_text(x = exp$voltage[which.max(exp$PP)] + 0.1, 
                y = max(exp$PP) + 10, label = "MPP", check_overlap = TRUE) +
      scale_colour_manual(values = c("red", "forestgreen"))
    

    【讨论】:

      【解决方案2】:

      你可以找到峰值然后 geom_vline 和 geom_hline 水平和垂直线。 峰值

      max(exp$PP)
      

      找到对应的 x 值。

      
      library(ggpmisc)
      draw <- ggplot(exp, aes(voltage)) + geom_line(aes(y = current , colour = "current")) + geom_line(aes(y=PP , colour= "PP"), linetype= 2) + scale_colour_manual(values=c("red", "green"))+annotate(geom = "point", x = 1.49, y = 521.937, colour = "orange", size = 3)+geom_vline(xintercept=1.49)+geom_hline(yintercept=521.937)
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-12
        • 2011-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 1970-01-01
        相关资源
        最近更新 更多