library(ggplot2) library(tidyverse)
December 14, 2022
library(ggplot2) library(tidyverse)
#make vector n <- c(2, 5.3, 9.7, 15.2)
Method 1
is.numeric(n)
## [1] TRUE
Method 2
class(n)
## [1] "numeric"
n
## [1] 2.0 5.3 9.7 15.2
n + 9
## [1] 11.0 14.3 18.7 24.2
n- 1.5
## [1] 0.5 3.8 8.2 13.7
n * 8
## [1] 16.0 42.4 77.6 121.6
i <- c(9, 11, 7, 5, 18) class(i)
## [1] "numeric"
But you can change that:
i <- as.integer(i) class(i)
## [1] "integer"
i_direct <- c(9L, 11L, 7L, 5L, 18L) i_direct class(i_direct)
## [1] 9 11 7 5 18 ## [1] "integer"
ch <- c("Make", "character", "vector")
ch
class(ch)
## [1] "Make" "character" "vector" ## [1] "character"
c_n <- c("5", "9", "11", "13")
class(c_n)
## [1] "character"
mean(c_n)
## Warning in mean.default(c_n): argument is not numeric or logical: returning NA
## [1] NA
numbers <- c(13, 8, 21, 37, 5) #Ask R if elements in vector are greater than 9 nine <- numbers > 9 nine
## [1] TRUE FALSE TRUE TRUE FALSE
#check class class(nine)
## [1] "logical"
l <- c(T, T, F, T,F) l
## [1] TRUE TRUE FALSE TRUE FALSE
#make numeric l <- as.numeric(l) l
## [1] 1 1 0 1 0
l <- as.logical(l) l
## [1] TRUE TRUE FALSE TRUE FALSE
sum(l)
## [1] 3
data <- data.frame(ID = c("Jane", "Matt", "Dan", "Karen", "Harold"),
age = c(35, 25, 30, 27, 29),
sex = c("female", "male", "male", "female", "male"))
## view data structure
str(data)
## 'data.frame': 5 obs. of 3 variables: ## $ ID : chr "Jane" "Matt" "Dan" "Karen" ... ## $ age: num 35 25 30 27 29 ## $ sex: chr "female" "male" "male" "female" ...
data$sex <- as.factor(data$sex) data$sex
## [1] female male male female male ## Levels: female male
# rest of data is unchanged str(data)
## 'data.frame': 5 obs. of 3 variables: ## $ ID : chr "Jane" "Matt" "Dan" "Karen" ... ## $ age: num 35 25 30 27 29 ## $ sex: Factor w/ 2 levels "female","male": 1 2 2 1 2
finish <- factor(c("first", "second", "fourth", "second", "first", "third", "fifth"))
finish
## [1] first second fourth second first third fifth ## Levels: fifth first fourth second third
finish <- factor(finish, levels = c("first", "second", "third", "fourth", "fifth"))
finish
## [1] first second fourth second first third fifth ## Levels: first second third fourth fifth
str(diamonds)
## tibble [53,940 × 10] (S3: tbl_df/tbl/data.frame) ## $ carat : num [1:53940] 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ... ## $ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ... ## $ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ... ## $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ... ## $ depth : num [1:53940] 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ... ## $ table : num [1:53940] 55 61 65 58 58 57 57 55 61 61 ... ## $ price : int [1:53940] 326 326 327 334 335 336 336 337 337 338 ... ## $ x : num [1:53940] 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ... ## $ y : num [1:53940] 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ... ## $ z : num [1:53940] 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
ggplot(diamonds)+ geom_violin(aes(x = cut, y = carat))
ggplot(diamonds)+ geom_violin(aes(x = cut, y = carat))
gplot(diamonds) + geom_violin(aes(x = cut, y = carat)) + coord_flip()
ggplot(diamonds) + geom_violin(aes(x = cut, y = carat)) + coord_flip()
ggplot(diamonds, aes(x = cut, y = carat)) + geom_violin() + stat_summary(fun = mean, geom = "point", shape = 8, size = 5, colour = "darkblue")
ggplot(diamonds, aes(x = cut, y = carat)) + geom_violin() + stat_summary(fun = mean, geom = "point", shape = 8, size = 5, colour = "darkblue")
stat <- function(x) {
ave <- mean(x)
ymin <- ave- sd(x)
ymax <- ave + sd(x)
return(c(y = ave, ymin = ymin, ymax = ymax))
}
ggplot(diamonds, aes(x = cut, y = carat)) + geom_violin() + stat_summary(fun.data = stat)
ggplot(diamonds, aes(x = cut, y = carat)) + geom_violin() + geom_boxplot(width = 0.1)
ggplot(diamonds, aes(x = cut, y = carat)) + geom_violin() + geom_boxplot(width = 0.1)
ggplot(diamonds, aes(x = cut, y = carat, colour = cut)) + geom_violin() + stat_summary(fun.data = stat)