setwd("C:/Users/Fei_lab/Documents/BTI2023")
library(ggplot2)
library(ggpubr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
x1 = rnorm(100)
x2 = rnorm(100)+rep(2,100)

jpeg("mytest.jpeg", height = 12, width = 12, units = "cm", res = 300)

par(mfrow=c(2,2))
#Make the plot
par(mar=c(0,5,3,3))
hist(x1 , main="" , xlim=c(-2,5), ylab="Frequency for x1", xlab="", ylim=c(0,30) , xaxt="n", las=1 , col="orange", breaks=10)
par(mar=c(5,5,0,3))
hist(x2 , main="" , xlim=c(-2,5), ylab="Frequency for x2", xlab="Value of my variable", ylim=c(25,0) , las=1 , col="tomato3"  , breaks=10)
par(mar=c(0,5,3,3))
hist(x1 , main="" , xlim=c(-2,5), ylab="Frequency for x1", xlab="", ylim=c(0,30) , xaxt="n", las=1 , col="darkgreen", breaks=10)

dev.off()
## png 
##   2
# library
library(ggplot2)
 
# The iris dataset is provided natively by R
#head(iris)
 
# basic scatterplot
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
    geom_point()

# Libraries
library(ggplot2)


# Load dataset from github
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/1_OneNum.csv", header=TRUE)

# Make the histogram
data %>%
  filter( price<300 ) %>%
  ggplot( aes(x=price)) +
    geom_density(fill="darkgreen", color="#e9ecef", alpha=0.8)

# Library
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.1     ✔ tidyr     1.3.0
## ✔ readr     2.1.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Data
a <- data.frame( x=rnorm(20000, 10, 1.9), y=rnorm(20000, 10, 1.2) )
b <- data.frame( x=rnorm(20000, 14.5, 1.9), y=rnorm(20000, 14.5, 1.9) )
c <- data.frame( x=rnorm(20000, 9.5, 1.9), y=rnorm(20000, 15.5, 1.9) )
data <- rbind(a,b,c)
 
 
# Basic scatterplot
ggplot(data, aes(x=x, y=y) ) +
  geom_point()

# Load ggplot2
library(ggplot2)

# Create data
data <- data.frame(
  name=c("A","B","C","D","E") ,  
  value=c(3,12,5,18,45)
  )

# Barplot
ggplot(data, aes(x=name, y = value, color = value, fill =  value)) +
  geom_bar(stat = "identity") +
  scale_color_gradient(low="blue", high="red") +
  scale_fill_gradient(low="blue", high="red") +
  xlab("X") +
  ylab("Y") +
  theme(legend.position = "top")

data <- data.frame(
  name=c("A","B","C","D","E") ,  
  value=c(3,12,5,18,45)
  )
par(mfrow=c(2,1))
pdf("scatterplot.pdf", height = 12, width = 12)

scatterplot <- ggplot(iris, aes(x = Sepal.Length, y=Sepal.Width, color = Sepal.Width)) +
  geom_point() +
  scale_color_gradient(low="blue", high="red") +
  annotate("rect", xmin = c(5, 7), xmax = c(6, 7.5), 
           ymin = c(2.5, 3.7), ymax = c(3, 4), alpha = 0.6, 
           color = "blue", fill = "blue") + 
  geom_rug(col = "steelblue", alpha = 0.1, size = 1.5) +
  theme(
    axis.ticks = element_line(size = 2, color = "darkgreen"),
    axis.text = element_text(angle = 0, color = "blue4", size = 10, face =2)
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
barplot <- ggplot(data, aes(x=name, y = value, color = value, fill =  value)) +
  geom_bar(stat = "identity") +
  scale_color_gradient(low="blue", high="red") +
  scale_fill_gradient(low="blue", high="red") +
  xlab("X") +
  ylab("Y") +
  theme(legend.position = "right")

scatter2 <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
    geom_point()

figure <- ggarrange(
              scatterplot, 
              ggarrange(scatter2, barplot, ncol = 2, labels = c("", "")), 
              nrow = 2, 
              labels = ""
          ) 
figure

Boxplot with significant test

Correlation

PCA