r/RStudio • u/jazzyj781 • Dec 15 '22
Need help making a boxplot
Hi, I'm very new to RStudio, for one of our projects we need to make a box plot, this is the data but I can't for the life of me figure it out. If anyone could tell me what to input into RStudioor how to restructure this data for RStudio it would mean a lot. Thank you!
11
u/PDubsinTF-NEW Dec 15 '22
Lots to unpack here. You are showing data in excel but asking for help in R.
I'd suggest you start by getting used to viewing data in R.
Steps that I take.
Create a project
Place data file into project folder
open new script
Determine which libraries you need. Probably readxl (for excel files) or haven (for spss files) and tidyverse (for nice looking data visualizations)
load libraries =
library(readxl)
library(tidyverse)
locate (via files tab) and load data
fulldata = read_excel("Master_Database.xlsx")
create boxplot
ggplot(fulldata) +
aes(x=group, y=variable of interest, fill=group,) +
geom_boxplot()
20
3
u/Old-Host-57 Dec 15 '22 edited Dec 15 '22
A boxplot won't show this data well. For example if you use the treatments as categories, you'd group all your measuring points per treatment whille you should aknowledge they are measured at different time points. Wetter you want to look at interesting things in your data or just learn to make a boxplot, your problem is your data format.
First, you can't have the rate in there like that, it is different data, you can't have it in your columns or R will take it and think it is one of the datapoints from that column. Secondly, R ussually likes long format, not wide.If you make this in long format instead of wide format (put your results in 1 column and the percentage in another column: yes, you'll repeat the percentages a lot) you could make a much nicer graph.
Something like ggplot(data, aes(x=time, y=results, col=percentage)+ geom_smooth(method=lm)+ geom_point()
Asumming you're already running tidyverse (or ggplot2 seperatly) of course.
In case you are a total beginner: you'll want to use the package ggplot2 for almost any simple graphs. Ggplot2 is included in the package tidyverse which allowes much easier data management and just really improves how R works. Google ggplot2 cheatsheet for a pdf with all the types of graphs it can do and their basic code. A simple boxplot code can be copied from there (or anywhere on the internet, remember, coders should be lazy copy past from the internet all the time).
2
u/OneCapital6836 Dec 15 '22
That’s the answer, reconfigure your data before try to built your graph.
1
u/JZOSS Dec 15 '22
Hi, in R there are two main ways to create boxplots: with the `boxplot` function (See https://r-coder.com/boxplot-r/ or https://r-charts.com/distribution/boxplot-function/) and with the geom_boxplot function from ggplot2 (https://r-charts.com/distribution/box-plot-ggplot2/). While `boxplot` can take a vector as input, ggplot2 always need a data frame. Probably the easiest way for someone new to R is to use the base function
1
u/DrewG4444 Dec 15 '22
I recommend getting the package ggplot2. You can use really helpful functions like geom_boxplot(). Do install.packages(“ggplot2”). Then library(ggplot2) and then you can use functions
14
u/PensiveOstrich Dec 15 '22
You can do it