首页 > 解决方案 > How to increase aesthetic qualities of ggplot?

问题描述

I'm currently working with some plotted data, and have managed to include a legend and diversify the y axis with different colours. Given all this, I would like one of the plots PP to be a dashed line, and include a black-point on the peak point with a line extending to the text MPP beside this point, along with lines extending from this point to the xy axis like so:

enter image description here

Here is my code thus far:

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

Reproducible example:

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

UPDATE
I've managed to add the dashed line.

标签: rggplot2

解决方案


You could draw geom_segments between the axes and the maximum of PP. The y co-ordinate at the maximum of PP can be found with max(exp$PP), and the x co-ordinate is at 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"))

enter image description here


推荐阅读