0
votes

enter image description hereI would like to plot box plots for a data set that including four categorical such as: Good, Bad, VeryGood, and VeryBad and four normal distribution. My question how to make plots the four categorical with four different normal distribution in one plot separate from each other, I have tried (see below) but there look a mess. I have used an example that I found it here and did some changes on it. I added another edited plot which I would like each plot of box plot look like this one which more cleat and each four categorical (blue, yellow, red and green) are clear . enter image description here

par(mfrow=c(2,2))
df <- data.frame(id = c(rep("Good",200), rep("Bad", 200),
                        rep("VeryGood",200), rep("VeryBad",200)),
           F1 = c(rnorm(200,10,2), rnorm(200,8,1), rnorm(200,5,2),rnorm(200,7,3)),
           F2 = c(rnorm(200,7,1),  rnorm(200,6,1), rnorm(200,8,1),rnorm(200,12,4)),
           F3 = c(rnorm(200,6,2),  rnorm(200,9,3),rnorm(200,12,3),rnorm(200,15,2)),
           F4 = c(rnorm(200,12,3), rnorm(200,8,2),rnorm(200,8,5),rnorm(200,5,1)))

boxplot(df[,-1], xlim = c(0.5, ncol(df[,-1])+0.9), 
        boxfill=rgb(1, 1, 1, alpha=1), border=rgb(1, 1, 1, alpha=1)) #invisible boxes
boxplot(df[which(df$id=="Good"), -1], xaxt = "n", add = TRUE, boxfill="red", boxwex=0.25, 
        at = 1:ncol(df[,-1]) - 0.15) #shift these left by -0.15
boxplot(df[which(df$id=="Bad"), -1], xaxt = "n", add = TRUE, boxfill="blue", boxwex=0.25,
        at = 1:ncol(df[,-1]) + 0.15) #shift these right by +0.15
boxplot(df[which(df$id=="VeryBad"), -1], xaxt = "n", add = TRUE, boxfill="green", boxwex=0.25,
        at = 1:ncol(df[,-1]) + 0.25) #shift these right by +0.15
boxplot(df[which(df$id=="VeryGood"), -1], xaxt = "n", add = TRUE, boxfill="yellow", boxwex=0.25,
        at = 1:ncol(df[,-1]) + 0.45) #shift these right by +0.15
1
It looks like all 4 of your plots are the same. Does it look better if you get rid of par(mfrow=c(2,2)) ?G5W
you are right @G5W, here like an example. I will use it for my real data which get similar idea.R. Saeiti
Can you post a desired output as it is not clear what the issue is? This might require you to use an image editor and sketch out a visual for us.Parfait
Agree that it's not clear what is desired. Different axis labeling? Titles?IRTFM
I think that you are saying that while these four are the same, when you use your real data, there will be four different plots, so you need to use par(mfrow=c(2,2)). Is that correct?G5W

1 Answers

1
votes

If you aren't set on using Base R graphics, and looking at the new plot you added to the question, I believe this is what you are looking for:

library(dplyr)
library(tidyr)
library(ggplot2)

df <- data.frame(id = c(rep("Good",200), rep("Bad", 200),
                        rep("VeryGood",200), rep("VeryBad",200)),
                 F1 = c(rnorm(200,10,2), rnorm(200,8,1), rnorm(200,5,2),rnorm(200,7,3)),
                 F2 = c(rnorm(200,7,1),  rnorm(200,6,1), rnorm(200,8,1),rnorm(200,12,4)),
                 F3 = c(rnorm(200,6,2),  rnorm(200,9,3),rnorm(200,12,3),rnorm(200,15,2)),
                 F4 = c(rnorm(200,12,3), rnorm(200,8,2),rnorm(200,8,5),rnorm(200,5,1)))

df2 <- tidyr::gather(df, key = "FVar", value = "value", F1:F4)

df2 %>% 
  ggplot(aes(id, value, fill = id)) + 
  geom_boxplot() + 
  facet_grid(. ~ FVar) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))

enter image description here