Need help with making scatterplots based on two variables in relation to another column

Hello

I am generally very new to R and for the life of me I cannot make two scatter plots that have x as (Value) and y as (Light) in relation to the (Molecule) column. Could someone please help me with this?

Sex Month Light Molecule Value
M Jaanuar 0.8 BDNF 50
M Veebruar 2 BDNF 80
M Marts 4.1 BDNF 290
M Aprill 6.2 BDNF 360
M Mai 8.9 BDNF 570
M Juuni 10.1 BDNF 1120
M Juuli 9 BDNF 600
M August 7.4 BDNF 400
M September 4.7 BDNF 280
M Oktoober 3 BDNF 195
M November 1 BDNF 20
M Juuni 10.1 Serotoniin 500
M Juuli 9 Serotoniin 500
M August 7.4 Serotoniin 285
M September 4.7 Serotoniin 378
M Oktoober 3 Serotoniin 225
M November 1 Serotoniin 500
M Detsember 0.5 Serotoniin 136
N Jaanuar 0.8 Serotoniin 802
N Veebruar 2 Serotoniin 252

It would be tough to know your exact problem without an example, but you can usually specify grouping variables in the aes argument with color = or fill =. There are some examples here

Some advice for making a reproducible example: here

2 Likes

Is this what you mean?

library(tidyverse)

# Your sample data on a copy/paste friendly format, replace this with your actual data frame
sample_df <- data.frame(
  stringsAsFactors = FALSE,
               Sex = c("M","M","M","M","M","M",
                       "M","M","M","M","M","M","M","M","M","M","M","M",
                       "N","N"),
             Month = c("Jaanuar","Veebruar","Marts",
                       "Aprill","Mai","Juuni","Juuli","August","September",
                       "Oktoober","November","Juuni","Juuli","August",
                       "September","Oktoober","November","Detsember","Jaanuar",
                       "Veebruar"),
             Light = c(0.8,2,4.1,6.2,8.9,10.1,9,
                       7.4,4.7,3,1,10.1,9,7.4,4.7,3,1,0.5,0.8,2),
          Molecule = c("BDNF","BDNF","BDNF","BDNF",
                       "BDNF","BDNF","BDNF","BDNF","BDNF","BDNF","BDNF",
                       "Serotoniin","Serotoniin","Serotoniin","Serotoniin",
                       "Serotoniin","Serotoniin","Serotoniin","Serotoniin",
                       "Serotoniin"),
             Value = c(50,80,290,360,570,1120,
                       600,400,280,195,20,500,500,285,378,225,500,136,
                       802,252)
)

# Relevant code
sample_df %>% 
    ggplot(aes(x = Value, y = Light)) +
    geom_point() +
    facet_wrap(facets = "Molecule")

Created on 2021-05-28 by the reprex package (v2.0.0)

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you for the help. I got the scatter plots from your code, but it wouldn't work anymore after I added some extra lines to it.

df <- read.delim("https://andreas.mindlabs.ee/kursused/itrakendused/data/arvestus/extra.txt")

library(ggplot2)
library(ggsignif)

ggplot(df, aes(x = Valgus, y = Vaartus)) +
geom_point(aes(shape = 21, size = 1)) +
facet_wrap(facets = "Molekul") +
geom_smooth(method = lm, se = FALSE, color = "darkorange") +
labs(x = 'Keskmine päiksevalgus (h)', y = 'Kogus proovis (μg)') +
lims(x = c(0,10.5)) +
theme_gray() +

Also made a small correlation test at the end, but the BDNF molecule one wouldn't work for some reason.

BDNF = subset(df, Molekul=="BDNF")
Serotoniin = subset(df, Molekul=="Serotoniin")
cor.test(BDNF$Valgus, BDNF$Vaartus)
cor.test(Serotoniin$Valgus, Serotoniin$Vaartus)

you put shape= within aes() making it try to map from the dataframe, instead of a fixed value so

  geom_point(size = 1,shape=21) +

_
also you have a trailing + after theme_gray()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.