简体   繁体   中英

Combine multiple columns in a dataframe for ggplot2 in R

I have a data frame below that I am trying to plot such that the values under Red , Blue , Green and MSAVI show on y-axis and Red , Blue , Green , MSAVI show on x-axis . And the class values show in the legend and the color can be defined according those character values too. Below is a sketch of what I am trying to make in R using ggplot2 .

How can I do this using dplyr ?

Class   Red         Blue        Green       MSAVI
GRND    0.254241894 0.110313222 0.159854318 -0.216356573
SHRB    0.081104881 0.042177001 0.069155373 0.127747396
TREE    0.092559343 0.050581477 0.083049583 0.08810719
WATR    0.09050273  0.034529627 0.060246605 -0.182429743

dput(profiles)
structure(list(Class = structure(1:4, .Label = c("GRND", 
"SHRB", "TREE", "WATR"), class = "factor"), Red = c(0.254241893688838, 
0.081104880819718, 0.0925593425830205, 0.0905027302602927), Blue = c(0.110313221812248, 
0.0421770010143518, 0.050581476961573, 0.034529626990358), Green = c(0.159854317704837, 
0.0691553726792336, 0.0830495829383532, 0.0602466048051914), 
    MSAVI = c(-0.216356573005517, 0.12774739585196, 
    0.0881071899784729, -0.182429743309816)), row.names = c(NA, 
-4L), class = "data.frame")

在此处输入图片说明

library(tidyverse)

# Read data
profiles = read.csv("~/profiles.csv)

# Plot using ggplot
profiles %>% 
  gather() %>% 
  ggplot(data = ., aes(x = fct_relevel(as.factor(key),
                                       levels = c("Red", 
                                                  "Blue",
                                                  "Green",
                                                  "MSAVI")), y = value, 
                           group=Class, color = Class)) +
  geom_point(size = 2.5) +
  geom_line(lwd = 1.2) +
  scale_color_manual(values=c('cyan', 'burlywood', 'darkgreen', 'blue')) +
  labs(title = "Spectral Profile from Multispectral Imagery",
       x = "Bands",
       y = "Reflectance") +
  #scale_y_continuous(limits=c(5000, 15000)) +
  theme(panel.background = element_blank(),
        panel.grid.major = element_line(color = "gray", size = 0.5),
        panel.grid.minor = element_line(color = "gray", size = 0.5),
        axis.ticks = element_blank())

In this case you want to use pivot_longer to combine the data.frame .

data %>% pivot_longer(
        cols = -"Class"
)

It gives you a long-format data.frame that collects all colums defined in cols . In this case I used negate , such that it combines all colums that are not Class , it gives,

# A tibble: 16 x 3
   Class name    value
   <fct> <chr>   <dbl>
 1 GRND  Red    0.254 
 2 GRND  Blue   0.110 
 3 GRND  Green  0.160 
 4 GRND  MSAVI -0.216 
 5 SHRB  Red    0.0811
 6 SHRB  Blue   0.0422
 7 SHRB  Green  0.0692
 8 SHRB  MSAVI  0.128 
 9 TREE  Red    0.0926
10 TREE  Blue   0.0506
11 TREE  Green  0.0830
12 TREE  MSAVI  0.0881
13 WATR  Red    0.0905
14 WATR  Blue   0.0345
15 WATR  Green  0.0602
16 WATR  MSAVI -0.182 

By default the values that are pivoted goes in value , and the colums in name .

data %>% pivot_longer(
        cols = -"Class"
) %>% ggplot(
        mapping = aes(x = name, y = value, color = Class, group = Class)
) + geom_line() + geom_point()

在此处输入图片说明

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