change theme, labels in ggplot2 with conditions


# library was used 

library(tidyverse)
library(readr)
library(ggplot2)

url <- "https://github.com/MohJumper/VisualAuditoryModality/blob/master/clean_test_master2.csv"
clean_test_master2 <- read.csv(url)

# code used 

clean_test_master2 %>%
  mutate(visbility_sound = case_when(
    visbility == 1 & soundvolume == 0 ~ "visibility",
    visbility == 0 & soundvolume == 1 ~ "soundvolume",
    visbility == 0 & soundvolume == 0 ~ "empty")) %>%
  mutate_at(vars(visbility_sound), factor) %>%
  ggplot(aes(x = stim_ending_t, y = m, color = visbility_sound)) +
  geom_line(aes(linetype = visbility_sound)) +
  geom_point() +
  facet_wrap(~Opening_text) +
  theme_minimal()

I want you to use the same plot as shown above in pic, while satisfying the conditions above.
Also, I want to know how to change the following labels as numbered in the pic below.

If someone can explain their answer, I would be super appreciative!

Inkednew_m_by_modality_LI

Check out this website which shows you how to change the plot title, the axis labels, and the legend title:
http://www.sthda.com/english/wiki/ggplot2-title-main-axis-and-legend-titles

This second page shows you how to add annotation at specific positions and labels for individual points:
http://www.sthda.com/english/wiki/ggplot2-texts-add-text-annotations-to-a-graph-in-r-software

For how to change the background colour, see here:
https://www.datanovia.com/en/blog/ggplot-theme-background-color-and-grids/

p.s. Some simple googling would have got you these results!

HTH

Thanks for your replay! I have found these before but when I tried to apply these steps, I do not get the things I want.

Here is where I got the plot shown above:
http://r-statistics.co/ggplot2-Tutorial-With-R.html

I tried this code but there are few things can not change yet e.g., The Opening_text (2), the legend (5), and the gray background as well as the "lm" is still colored not gray

new_ce %>%
  mutate(visbility_sound = case_when(
    visbility == 1 & soundvolume == 0 ~ "Visual",
    visbility == 0 & soundvolume == 1 ~ "Auditory",
    visbility == 0 & soundvolume == 0 ~ "Empty")) %>%
  mutate_at(vars(visbility_sound), factor) %>%
  ggplot(aes(x = stim_ending_t, y = m, color = visbility_sound)) +
  geom_line(aes(linetype = visbility_sound)) +
  geom_point(aes(color = visbility_sound, shape = visbility_sound))+
  stat_smooth(aes(color = visbility_sound, fill = visbility_sound), 
              method = "lm", alpha = 0.1) +
  xlab("Sample Durations in Seconds") + ylab("Mean responses") +
  facet_wrap(~Opening_text) +
  theme_minimal()

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

Hi @FJCC ,

I have a similar question that had not been answered here

I have made so much progress but there is only two things that I still need help with. For the purpose to keep my replay here focused on the same topic, one of my questions is about how to change the the content of facet_grid(~type+morphotype) ?

Here is a regex and my question is how to change the content of facet_wrap(~Opening_text) to "Now Attend to Visual" , "Now Attend to Auditory"

# library was used 

library(tidyverse)
library(readr)
library(ggplot2)

url <- "https://github.com/MohJumper/VisualAuditoryModality/blob/master/clean_test_master2.csv"
clean_test_master2 <- read.csv(url)

# code used 
clean_test_master2 %>%
    mutate(visbility_sound = case_when(
      visbility == 1 & soundvolume == 0 ~ "Visual",
      visbility == 0 & soundvolume == 1 ~ "Auditory",
      visbility == 0 & soundvolume == 0 ~ "Empty")) %>%
    mutate_at(vars(visbility_sound), factor) %>%
    ggplot(aes(x = stim_ending_t, y = m, color = visbility_sound)) +
    geom_line(aes(linetype = visbility_sound)) +
    geom_point(aes(color = visbility_sound, shape = visbility_sound))+
    xlab("Sample Durations in Seconds") + ylab("Mean responses") +
    theme_bw() +
    theme(legend.background = element_rect(fill = "darkgray"),
      legend.key = element_rect(fill = "white", color = NA),
      legend.key.size = unit(1.9, "cm"),
      axis.title.y=element_text(size=15),
      axis.text.x=element_text(size=15),
      legend.key.width = unit(0.01,"cm")) +
    facet_wrap(~Opening_text)

Note: if this is something that should not be posted here please let me know and I will happily delete my post.
Thanks

I suspect this should not have been asked here, but I will answer and perhaps someone will move the two posts.
If I understand you correctly, I would simply make a new column mapping the old Opening_text values to the desired new ones.
Notice that I changed the url.
If this does not anser your question, please do make a new thread.

library(dplyr)
library(readr)

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3

#url <- "https://github.com/MohJumper/VisualAuditoryModality/blob/master/clean_test_master2.csv"
url <- "https://raw.githubusercontent.com/MohJumper/VisualAuditoryModality/master/clean_test_master2.csv"
clean_test_master2 <- read.csv(url)

# code used 
clean_test_master2 %>%
  mutate(visbility_sound = case_when(
    visbility == 1 & soundvolume == 0 ~ "Visual",
    visbility == 0 & soundvolume == 1 ~ "Auditory",
    visbility == 0 & soundvolume == 0 ~ "Empty")) %>%
  mutate_at(vars(visbility_sound), factor) %>%
  mutate(Opening_text_new = ifelse(Opening_text == "Now focus on the Image", "Now Attend to Visual",
                                   "Now Attend to Auditory")) %>% 
  ggplot(aes(x = stim_ending_t, y = m, color = visbility_sound)) +
  geom_line(aes(linetype = visbility_sound)) +
  geom_point(aes(color = visbility_sound, shape = visbility_sound))+
  xlab("Sample Durations in Seconds") + ylab("Mean responses") +
  theme_bw() +
  theme(legend.background = element_rect(fill = "darkgray"),
        legend.key = element_rect(fill = "white", color = NA),
        legend.key.size = unit(1.9, "cm"),
        axis.title.y=element_text(size=15),
        axis.text.x=element_text(size=15),
        legend.key.width = unit(0.01,"cm")) +
  facet_wrap(~Opening_text_new)

Created on 2019-10-27 by the reprex package (v0.3.0.9000)

1 Like

That is exactly what I wanted! Thanks again, and hopefully that was helpful to other people .