0

I'm trying to plot a parameter (i.e. RMSE) with three classes (first: land cover; second:bands, third: names). A similar question was posted here, but the y-axis is not a variable in dataframe.
I have tried as following:

library(ggplot2)
library(ggpattern)

df_data <- data.frame(type = c('Forest','Forest','Forest','Forest','Forest','Forest',
                          'Shrub','Shrub','Shrub','Shrub','Shrub','Shrub'),
                 Band = c('A','B','C','A','B','C','A','B','C','A','B','C'),
                 Name = c('N1','N2','N1','N2','N1','N2','N1','N2','N1','N2','N1','N2'),
                 RMSE = c(54.8,58.3,58.7,46.5,42.9,44.7,34.6,39.9,45.1,31.9,33.9,38.3))

df_data$type = factor(df_data$type, levels = c("Forest", "Shrub"))
df_data$Band = factor(df_data$Band, levels = c("A","B","C"))
df_data$Name = factor(df_data$Name, levels = c("N1","N2"))

theme_set(theme_bw())

p1 <- ggplot(df_data, aes(x = type, y = RMSE)) + 
  geom_col_pattern(position = position_dodge(width = 0.95), width = 0.8,
                   aes(pattern_fill = Band, fill = Name, pattern_angle = Band),
                   pattern_density = 0.1, pattern_size = 0.15) + 
  scale_fill_manual(values = c("#80B1D3", "#9CDC82", "#BEBADA", "#FDB462", "#FB8072", "#8DD3C7"))+
  labs(x="", y="RMSE") + ylim(0, 60)

But the result is strange.

Plot Output

First, the second class "Bands" should be fill with stripes (without color), and the third class "Name" should be fill with color (without stripes). This is the figure I want to plot, which is similar to this question.

changkx
  • 13
  • 5
  • 1
    Please define *strange*. And what is length and content of `RMSE`? Per `r` tag (hover or click to see): Please provide minimal and [reproducible example(s)](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) along with the desired output. Use `dput()` for data and specify all non-base packages with `library()` calls. And please avoid data links for readers denied GDrive paths by corporate proxies or future readers with dead link. – Parfait Nov 21 '20 at 05:12
  • 1
    Difficult to get your data into my R. Please review https://stackoverflow.com/help/minimal-reproducible-example. You can subset your data (maybe 5 rows is good enough?) and add that using `dput(dt_data)`. Paste the `dput` results into your question. Good luck! – Eric Krantz Nov 21 '20 at 05:23
  • @Parfait Thank you, Parfait and Eric Krantz. Thanks for your kindly reminder. I have update my question and make it clearly to understand. – changkx Nov 23 '20 at 03:43
  • Where are *second class* and *third class* identified in plot? Are they the legend series, A-C and N1-N6? Are you saying legend should be reversed with latter not having stripes? Maybe use image software (outside of R) to show ideal plot or circle the problem areas. Also, `RMSE` is a column in data frame so how can this be true: *but the y-axis is not a variable in dataframe* – Parfait Nov 23 '20 at 04:06
  • @Parfait Sorry for the confused question. Yes. the _second and third_ class are legend series, A-C and N1-N6. The legend A-C should only be filled with strips. The legend N1-N6 should not be having strips. In this case, I want to use RMSE as y-axis. But in the similar question I post [link](https://stackoverflow.com/questions/62393159/how-can-i-add-hatches-stripes-or-another-pattern-or-texture-to-a-barplot-in-ggp), its y-axis is not a column in dataframe. – changkx Nov 23 '20 at 07:08
  • Understood. From linked solution, try adding `fill` and `pattern` args in `aes` of `ggplot` and remove `aes` in `geom_col_pattern`: `ggplot(df_data, aes(x = type, y = RMSE, fill=Name, pattern=Band)) ...` – Parfait Nov 23 '20 at 18:20
  • @Parfait Thanks for your patience and support. I successfully completed this following your suggestion. – changkx Nov 28 '20 at 09:42

1 Answers1

1

I successfully completed this following the suggestion of @Parfait.

library(ggplot2)
library(ggpattern)
# remotes::install_github("coolbutuseless/ggpattern")

df_data <- data.frame(type = c('Forest','Forest','Forest','Forest','Forest','Forest',
                               'Shrub','Shrub','Shrub','Shrub','Shrub','Shrub'),
                      Band = c('A','B','C','A','B','C','A','B','C','A','B','C'),
                      Name = c('N1','N2','N1','N2','N1','N2','N1','N2','N1','N2','N1','N2'),
                      RMSE = c(54.8,58.3,58.7,46.5,42.9,44.7,34.6,39.9,45.1,31.9,33.9,38.3))

df_data$type = factor(df_data$type, levels = c("Forest", "Shrub"))
df_data$Band = factor(df_data$Band, levels = c("A","B","C"))
df_data$Name = factor(df_data$Name, levels = c("N1","N2"))

theme_set(theme_bw())

ggplot(df_data, aes(x = type, y = RMSE, fill = Name, pattern = Band, pattern_angle = Band)) + 
    geom_col_pattern(position = position_dodge(preserve = "single"),
                   color = "black", pattern_fill = "black",
                   pattern_density = 0.001, pattern_spacing = 0.025, pattern_key_scale_factor = 0.6) + 
    scale_fill_manual(values = c("#80B1D3", "#9CDC82"))+
    scale_pattern_manual(values = c("none", "stripe", "stripe")) +
    scale_pattern_angle_manual(values = c(0, 45, 0)) +
    labs(x="", y="RMSE") + ylim(0, 60) + 
    guides(pattern = guide_legend(override.aes = list(fill = "white")),
           fill = guide_legend(override.aes = list(pattern = "none"))) +
    theme(axis.text.y = element_text(size = 30, hjust = 0.5, color = "black"), 
          axis.text.x = element_text(size = 30, color = "black"),
          axis.title.y = element_text(size = 30, face = "bold", vjust = 1.5),
          legend.title = element_blank(), legend.text = element_text(size = 30, face = "bold"))

Plots

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
changkx
  • 13
  • 5