Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to scatterplot based different column conditions in r?

My dataframe looks like this:

dput(B8A)
structure(list(ID = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", 
"LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "P0", "P01", "P02", 
"P1", "P18", "P2", "P22", "P23", "P24", "P3", "P4"), nD_SOS = c(129L, 
349L, 256L, 319L, 306L, 128L, 309L, 244L, 134L, 356L, 131L, 302L, 
276L, 296L, 294L, 310L, 337L, 291L, 308L, 295L, 308L, 303L, 310L
), nNDVI_SOS = c(119L, 374L, 272L, 329L, 328L, 152L, 317L, 246L, 
129L, 378L, 152L, 315L, 358L, 314L, 310L, 305L, 363L, 305L, 281L, 
274L, 285L, 302L, 311L), crop = c("sunfl", "wwht", "wrap", "wwht", 
"fbn", "sunfl", "wwht", "wmust_vetch", "irr_corn", "wwht", "irr_corn", 
"wwht", "wrap", "wrap", "wrap", "wrap", "wwht", "wrap", "wrap", 
"wrap", "wrap", "wrap", "wrap"), year = c(2016, 2016, 2017, 2018, 
2019, 2020, 2015, 2016, 2017, 2017, 2019, 2019, 2016, 2016, 2016, 
2017, 2018, 2017, 2015, 2015, 2015, 2017, 2018)), row.names = c(NA, 
-23L), class = "data.frame")

I want to do a scatterplot between nD_SOS and nNDVI_SOS based on different column conditions. The condition is to get the scatterplot only for the year 2017 and the crop wrap.

I’ve been using this code for plotting:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

p3 <- ggplot(nB8A_SOS_KRR25_crop, aes(x=nNDVI_SOS, y=nD_SOS)) +
  geom_point() +
  stat_regline_equation(label.x=30, label.y=310) +
  stat_cor(aes(label=..rr.label..), label.x=30, label.y=290)+
  geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE) +
  ggtitle("B8A KRR 25") +
  theme_ipsum()

p3

The idea is to add a piece of code to show the data for the specific year and crop.

Any help will be much appreciatted.

>Solution :

df %>%  
  filter(year == 2017, 
         crop == "wrap") %>% 
  ggplot() + 
  aes(x = nNDVI_SOS, y = nD_SOS) + 
  geom_point() + 
  stat_regline_equation(label.x=30, label.y=310) +
  stat_cor(aes(label=..rr.label..), label.x=30, label.y=290)+
  geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE) +
  ggtitle("B8A KRR 25") +
  theme_ipsum()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading