_____________________________________________________________________

Linux Day a Pisa 2019

26 October 2019 Pisa

_____________________________________________________________________

R Graphics Script

variables x and y

### variables x and y
x <- seq(from = 0.1, to = 10, by = 0.1)
x
##   [1]  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0  1.1  1.2  1.3  1.4
##  [15]  1.5  1.6  1.7  1.8  1.9  2.0  2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8
##  [29]  2.9  3.0  3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.0  4.1  4.2
##  [43]  4.3  4.4  4.5  4.6  4.7  4.8  4.9  5.0  5.1  5.2  5.3  5.4  5.5  5.6
##  [57]  5.7  5.8  5.9  6.0  6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9  7.0
##  [71]  7.1  7.2  7.3  7.4  7.5  7.6  7.7  7.8  7.9  8.0  8.1  8.2  8.3  8.4
##  [85]  8.5  8.6  8.7  8.8  8.9  9.0  9.1  9.2  9.3  9.4  9.5  9.6  9.7  9.8
##  [99]  9.9 10.0
length(x)
## [1] 100
set.seed(10)
y <- sample(x = 1:30, size = 100, replace = TRUE)
y
##   [1] 11  9 10 16 12 23  8 22  7 19 24 15 15 10  7 10  2 24 13  8 14  7  6
##  [24]  7 27 22 18 29 29 21 25 28 18 13 26  5 26  1  7 26 18 30 25  4 29 18
##  [47] 24 19 22 27 18 11 15  1 10 28 27 10 14 13  1  9 25  8 16 21 26 25 17
##  [70] 16 23 15 30 14 24  2 29  4  3 14  1  3 10 10 26  7 16 15 10  5 19 21
##  [93]  5 13 15  7 19 22 25 18
length(y)
## [1] 100

R base —> plot()

#### R base ---> plot() 
plot(x, y)

type = " "

## type = "" 
plot(x, y, type = "l")

## type = "" 
plot(x,y, type = "o")

## type = "" 
plot(x, y, type = "b")

## type = "" 
plot(x,y, type = "n")

col =

##col 
plot(x, y, type = "l", col = 2)

##col
plot(x, y, type = "l", col = "blue")

##col 
plot(x,y, type = "l", col = "DarkOrange")

main = sub = xlab = ylab = cex

plot(x, y, type = "l", col = 4, 
     main = "Title",
     sub = "subtitle",
     xlab = "var x label", 
     ylab = "var y label",
     cex.lab = 0.8
     )

cex

cex

lty = lwd =

####################
plot(x, y, type = "l", col = 4, 
     lty = 2, lwd = 2,     ## lty lwd
     main = "Title", sub = "subtitle", xlab = "var x label", ylab = "var y label")

lty lwd

lty lwd

points() pch =

####################
plot(x, y, type ="l", col = 4, lty = 2, lwd = 2,
     main = "Title", sub = "subtitle",
     xlab = "var x label", ylab = "var y label")
points(x = x, y = y, col = "green", pch = 15) #point() pch =

pch

pch

—————————–

Tutorial 5.1 - Traditional R Graphics

http://www.flutterbys.com.au/stats/tut/tut5.1.html link screenshot

—————————–

abline()

####################
plot(x, y, type = "l", col = 4, lty = 2, lwd = 2,
     main = "Title", sub = "subtitle",
     xlab = "var x label", ylab = "var y label")
points(x = x, y = y, col = "green", pch = 15)
abline(h = median(y), lty = 2, lwd = 1.5, col = "red") #abline() horizontal
abline(v = median(x), lty = 2, lwd = 1.5, col = "red") #abline() vertical

legend()

####################
plot(x, y, type = "l", col = 4, lty = 1, lwd = 1.5, main="Title",
     sub = "subtitle", xlab = "var x label", ylab = "var y label")
points(x = x, y = y, col = "green", pch = 15)
abline(h = median(y), lty = 2, lwd = 1.5, col = "red") 
abline(v = median(x), lty = 2, lwd = 1.5, col = "red") 
legend("topright", legend = c("line1"), #legend
       col = c("blue"), lty = 1,
       box.lty = 1, cex = 0.6)

par() —> mfrow= or mfcol= c(rows, coloums)

## par parameters
par(mfrow=c(2,2)) 
 
plot(x,y)
plot(x,y, type="l")
plot(x,y, type="o")
plot(x,y, type="h")

par() col = " "

## par() col = ""
par(mfrow=c(2,2))
plot(x,y)
plot(x,y, type="l", col=2)
plot(x,y, type="o", col="blue")
plot(x,y, type="h", col="Darkorange")

4 graphs!

####################
par(mfrow=c(2,2), cex = 0.8)
p <- c(1, 19, 18, 13) # pch 
for (i in 1:4) {
  plot(x,y, type="l", col=4, main="Graph", sub="type=\"l\"",
       xlab="variable X", ylab="var Y")
  index.M <- which(y %in% y[y>15])
  index.m <- which(y %in% y[y<15])
  points(x=x[index.M], y=y[index.M], col="green", pch=p[i])
  points(x=x[index.m], y=y[index.m], col="red", pch=p[i])
  abline(h=15, lty=2, lwd=1.5, col="gray")
}

create Data Frame df

## x
x
##   [1]  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0  1.1  1.2  1.3  1.4
##  [15]  1.5  1.6  1.7  1.8  1.9  2.0  2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8
##  [29]  2.9  3.0  3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.0  4.1  4.2
##  [43]  4.3  4.4  4.5  4.6  4.7  4.8  4.9  5.0  5.1  5.2  5.3  5.4  5.5  5.6
##  [57]  5.7  5.8  5.9  6.0  6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9  7.0
##  [71]  7.1  7.2  7.3  7.4  7.5  7.6  7.7  7.8  7.9  8.0  8.1  8.2  8.3  8.4
##  [85]  8.5  8.6  8.7  8.8  8.9  9.0  9.1  9.2  9.3  9.4  9.5  9.6  9.7  9.8
##  [99]  9.9 10.0
## y
y
##   [1] 11  9 10 16 12 23  8 22  7 19 24 15 15 10  7 10  2 24 13  8 14  7  6
##  [24]  7 27 22 18 29 29 21 25 28 18 13 26  5 26  1  7 26 18 30 25  4 29 18
##  [47] 24 19 22 27 18 11 15  1 10 28 27 10 14 13  1  9 25  8 16 21 26 25 17
##  [70] 16 23 15 30 14 24  2 29  4  3 14  1  3 10 10 26  7 16 15 10  5 19 21
##  [93]  5 13 15  7 19 22 25 18
####################
df <- data.frame(x = x, 
                 y = y)
df$test <- ifelse(df$y > 15, 1, 0)
df$test <- as.factor(df$test)
####################

head(df)
dim(df)
## [1] 100   3
str(df)
## 'data.frame':    100 obs. of  3 variables:
##  $ x   : num  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 ...
##  $ y   : int  11 9 10 16 12 23 8 22 7 19 ...
##  $ test: Factor w/ 2 levels "0","1": 1 1 1 2 1 2 1 2 1 2 ...

create simple plot()

####################
plot(x = df$x, 
     y = df$y, 
     type = "l", 
     col = 4, 
     main = "Graph temperatures",
     sub = "critical values temp.>25°",
     xlab = "time", 
     ylab = "temperatures",
     cex.lab = 0.8
    )

add points green and red

####################
plot(x = df$x, 
     y = df$y, 
     type = "l", 
     col = 4, 
     main = "Graph temperatures",
     sub = "critical values temp.>25°",
     xlab = "time", 
     ylab = "temperatures",
     cex.lab = 0.8
    )

## add points green and red
points(x = df$x[df$test == 1],
       y = df$y[df$test == 1], 
       col = "green", pch = 19)

points(x = df$x[df$test == 0],
       y = df$y[df$test == 0],
       col = "red", pch = 19)

add line gray y=15

####################
plot(x = df$x, 
     y = df$y,
     type = "l", 
     col = 4, 
     main = "Graph temperatures",
     sub = "critical values temp.>25°",
     xlab = "time", 
     ylab = "temperatures",
     cex.lab = 0.8
    )

## add points green and red
points(x = df$x[df$test == 1],
       y = df$y[df$test == 1], 
       col = "green", pch=19)
points(x = df$x[df$test == 0],
       y = df$y[df$test == 0],
       col = "red", pch=19)

## add line gray y=15
abline(h = 15, lty = 2, lwd = 1.5, col = "gray")

add coordinates labels

####################
plot(x = df$x, 
     y = df$y,
     type = "l", 
     col = 4, 
     main = "Graph temperatures",
     sub = "critical values temp.>25°",
     xlab = "time", 
     ylab = "temperatures",
     cex.lab = 0.8
    )

## add points green and red
points(x = df$x[df$test == 1],
       y = df$y[df$test == 1], 
       col = "green", pch=19)
points(x = df$x[df$test == 0],
       y = df$y[df$test == 0],
       col = "red", pch=19)

## add line gray y=15
abline(h = 15, lty = 2, lwd = 1.5, col = "gray")

## add coordinates labels
text(x = df$x, y = df$y+0.5,
     paste(df$x, df$y, sep=","), 
     cex = 0.4, font = 2)

add red line critical values

#####################
plot(x = df$x, 
     y = df$y, 
     type = "l", 
     col = 4, 
     main = "Graph temperatures",
     sub = "critical values temp.>25°",
     xlab = "time", 
     ylab = "temperatures",
     cex.lab = 0.8
    )

## add points green and red
points(x = df$x[df$test == 1],
       y = df$y[df$test == 1], 
       col = "green", pch = 19)
points(x = df$x[df$test == 0],
       y = df$y[df$test == 0],
       col = "red", pch = 19)

## add line gray y=15
abline(h = 15, lty = 2, lwd = 1.5, col = "gray")

## add coordinates labels
text(x = df$x,y = df$y+0.5,
     paste(df$x, df$y, sep = ","), 
     cex = 0.4, font = 2)

## add red line critical values
abline(h = 25, col="red", lty = 2)

add arrows in max values y

####################
plot(x = df$x, 
     y = df$y, 
     type = "l", 
     col = 4, 
     main = "Graph temperatures",
     sub = "critical values temp.>25°",
     xlab = "time", 
     ylab = "temperatures",
     cex.lab = 0.8
    )

## add points green and red
points(x = df$x[df$test == 1],
       y = df$y[df$test == 1], 
       col = "green", pch=19)
points(x = df$x[df$test == 0],
       y = df$y[df$test == 0],
       col = "red", pch=19)

## add line gray y=15
abline(h = 15, lty = 2, lwd = 1.5, col = "gray")

## add coordinates labels
text(x = df$x, y = df$y+0.5,
     paste(df$x, df$y, sep = ","), 
     cex = 0.4, font = 2)

## add red line critical values
abline(h = 25, col = "red", lty = 2)

## add arrows in max values y
############# code = 1 --> 
#############        2 <--
#############        3 <--> 
arrows(x0=df$x[y == max(df$y)]+0.1, # x from
       y0=df$y[y == max(df$y)]+0.1, # y from 
       x1=df$x[y == max(df$y)]+0.5, # x to     
       y1=df$y[y == max(df$y)]+0.5, # y to
       length = 0.1, angle = 30, 
       code = 1, col = 4, lwd = 2)

add lines summary()

####################
plot(x = df$x, 
     y = df$y,
     type = "l", 
     col = 4, 
     main = "Graph temperatures",
     sub = "critical values temp.>25°",
     xlab = "time", 
     ylab = "temperatures",
     cex.lab = 0.8
    )

## add points green and red
points(x = df$x[df$test == 1],
       y = df$y[df$test == 1], 
       col = "green", pch = 19)
points(x = df$x[df$test == 0],
       y = df$y[df$test == 0],
       col = "red", pch = 19)

## add line gray y=15
abline(h = 15, lty = 2, lwd = 1.5, col = "gray")

## add coordinates labels
text(x = df$x,y = df$y+0.5,
     paste(df$x, df$y, sep = ","), 
     cex = 0.4, font = 2)

## add red line critic values
abline(h = 25, col = "red", lty = 2)

## add arrows in max values y
############# code = 1 --> 
#############        2 <--
#############        3 <--> 
arrows(x0 = df$x[y == max(df$y)]+0.1, # x from
       y0 = df$y[y == max(df$y)]+0.1, # y from 
       x1 = df$x[y == max(df$y)]+0.5, # x to     
       y1 = df$y[y == max(df$y)]+0.5, # y to
       length = 0.1, angle = 30, 
       code = 1, col = 4, lwd = 2)

##add lines summary()
abline(h = summary(y), lty = 4, col = "purple")

summary(y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    9.00   15.00   15.72   23.25   30.00

plot(type = “n”) rect()

plot(c(0, 120), c(0, 120), type= "n", main = "graph", 
     xlab = "", ylab = "", xaxt = "n", yaxt = "n")
rect( 10, 0, 40, 50, 
      col = 2, border = "gray")
rect( 40, 0, 70, 50, 
      col = 4, border = "gray")
rect( 25, 50, 55, 100, 
      col = 3, border = "gray")

boxplot()

##########
boxplot(y)

boxplot()

##########
boxplot(y, horizontal = TRUE)

boxplot()

(?fivenumber: Tukey Five-Number Summaries: Returns Tukey’s five number summary (minimum, lower-hinge, median, upper-hinge, maximum) for the input data.)

##########
fivenum(y)
## [1]  1.0  9.0 15.0 23.5 30.0
boxplot(y)
abline(h = fivenum(y))
text(x = 0.5, 
     y = fivenum(y)+0.6,
     labels = fivenum(y), cex = 0.8
    )

##########
summary(y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    9.00   15.00   15.72   23.25   30.00
boxplot(y)
abline(h = summary(y))
text(x = 0.5, 
     y = summary(y)+0.6,
     labels = summary(y), cex = 0.8
    )

boxplot() abline()

##########
boxplot(y, horizontal = FALSE, border = "blue")
abline(h = fivenum(y), col = "Darkred", lty = 2, lwd = 0.5)
text(x = 0.5, 
     y = fivenum(y)+0.5, 
     labels = fivenum(y), 
     font = 2, cex = 0.8, col = "gray")

save file svg

————————————-

svg("~/Scrivania/graph1.svg")

code graph .....

dev.off()

————————————

iris Dataset

wikipedia

wikipedia

RPubs

RPubs

car package –> scatterplot()

##########
#install.packages("RCurl")
#### terminal --> apt-get install libcurl4-openssl-dev 
#install.packages("car", dependencies = TRUE)
library(car)
## Loading required package: carData
scatterplot(Sepal.Length ~ Petal.Length,
            data = iris, col=2)

##########
#library(car)
scatterplot(Sepal.Length ~ Petal.Length | Species, 
            data = iris, 
            pch = c(20 , 20, 20), 
            col = c(1, 2, 3))

#regression line for versicolor Species
lm.versicolor <- lm(Sepal.Length ~ Petal.Length, 
        data = subset(iris, Species == "versicolor"))

scatterplot(Sepal.Length ~ Petal.Length | Species, 
            data = iris, 
            pch = c(20, 20, 20), 
            col = c(1, 2, 3))
#abline(lm.versicolor) 
abline(lm.versicolor, col = "Darkred", lty = 2)

R base Plot()

##########
lm.setosa <- lm(Sepal.Length ~ Petal.Length, 
                data = subset(iris,Species == "setosa"))
lm.versicolor <- lm(Sepal.Length ~ Petal.Length, 
                    data = subset(iris,Species == "versicolor"))
lm.virginica <- lm(Sepal.Length ~ Petal.Length, 
                   data = subset(iris,Species == "virginica"))


plot(Sepal.Length ~ Petal.Length, 
     data = iris, 
     col = Species, 
     pch = 20)
legend("bottomright", legend = names(table(iris$Species)), 
       title="Iris", fill = c(1,2,3), cex=0.8)

abline(lm.setosa, col="black", lty = 2)
abline(lm.versicolor, col = "Darkred", lty = 2)
abline(lm.virginica, col = "green", lty = 2)

scatterplot() car pkg ellipse = TRUE

##########
#library(car)
scatterplot(Sepal.Length ~ Petal.Length | Species, 
            data = iris, 
            pch = c(20, 20, 20), 
            col = c(1, 2, 3), 
            ellipse = TRUE)

Pie Graph

##################
# link https://www.statmethods.net/graphs/pie.html
mytable <- table(iris$Species)
lbls <- paste(names(mytable), "\n", mytable, sep="")
pie(mytable, 
    labels = lbls,
     main="Pie Chart of Species\n 
     (with sample sizes)")

hist() vector

a <- c(0.407143173, -1.405579071,  2.172244168, -0.539542776,  0.486887772,
      -0.261532000,  0.923313589, -0.910911965,  0.983827544,  1.003069942,
      -0.790760348, -1.658033573,  0.400994752, -2.348004883,  0.730477590,
       0.755255204,  0.788566380, -0.342063446,  1.823848892, -0.148140480,
      -0.971565897, -0.389138968, -1.263656166, -1.278265506,  0.204386956,
       0.046567748, -0.908244134, -0.218683231, -0.155295420, -0.306256372,
       1.769289381, -0.593041995,  0.689431248, -0.340080187, -1.061509942,
      -0.939173204,  0.156912588, -0.571797205,  0.158199488,  0.340690847,
       0.735364418, -1.024828130,  1.363362141, -1.915180723, -1.923833752,
       1.135716960,  1.241223614, -1.157504888,  0.496621993, -1.331908129,
       1.592628620,  0.061958081, -0.145945861, -0.343735779, -1.247170993,
       0.843333960, -0.002131136,  0.491880850, -0.814033523, -0.430486011,
       0.236554399, -1.754225843, -0.952783830, -0.628854148,  0.562778893,
      -0.953274417, -1.094557409,  0.411517133, -0.766182568,  1.006729041,
      -1.698899460, -0.384019443, -0.808773130, -0.524889374,  0.714887190,
       0.702029289, -0.768700129, -1.877124097, -0.770522589,  1.333966526,
      -0.020293545, -0.881977272,  0.008728021,  0.822826091,  1.865289904,
       0.541310354, -0.990387611,  2.277077020,  0.088089367,  1.421035780,
       1.301798210, -2.037916075,  1.640861968, -0.109926100, -0.837959393,
       0.074671122,  0.081318426,  0.025691173, -0.147900751,  1.502843673)
hist(a, freq = FALSE)

hist(a, freq = FALSE)
curve(dnorm(x, 
            mean = mean(a), 
            sd = sd(a)), 
            add = TRUE, #if TRUE add to an already existing plot
            col = 2)

lattice

?lattice

The ‘lattice’ add-on package is an implementation of Trellis graphics for R. It is a powerful and elegant high-level data visualization system with an emphasis on multivariate data. It is designed to meet most typical graphics needs with minimal tuning, but can also be easily extended to handle most nonstandard requirements.

Trellis Graphics, originally developed for S and S-PLUS at the Bell Labs, is a framework for data visualization developed by R.

xyplot()

#install.packages("lattice")
library(lattice)
xyplot(Sepal.Length ~ Petal.Length, 
       data=iris, 
       auto.key = TRUE,
       col=2)

#library(lattice)
xyplot(Sepal.Length ~ Petal.Length, 
       data = iris, 
       col = 2,
       type = c("p", "g", "smooth")) #p=points, l=lines, r=regression line, smooth=loess fit, g=grid, ...

dotplot() / xyplot()

#library(lattice)
dotplot(Sepal.Length~Petal.Length | Species, #xyplot
        group = Species,
        data = iris, 
        auto.key = TRUE, #legend
        type = c("p", "g"),  
        scales=list(cex=.5, col="red"), #List providing axis annotation information
        pch = c(1, 1, 1),
        col = c(1, 2, 3)
       )

#library(lattice)
dotplot(y ~ x | test, 
        group = test,
        data = df, 
        aspect = 1, # aspect ratio (height/width)
        layout = c(2,1), #c(coloumns, rows)
        auto.key = TRUE,
        Scales = TRUE,
        type = c("p", "g"),
        pch = c(1, 3),
        col = c("red", "green"),
        main = "Graph temperatures",
        xlab = "time",
        ylab = "temperatures"
       )

#library(lattice)
dotplot(y ~ x | test, 
        group = test,
        data = df, 
        #aspect = 1,
        layout = c(1, 2),
        auto.key = TRUE,
        scales=list(cex=.5, col="gray"),
        type = c("p", "g"),
        pch = c(1, 3),
        col = c("red", "green"),
        main = "Graph temperatures",
        xlab ="time",
        ylab = "temperatures"
       )

histogram()

#library(lattice)
histogram(~Sepal.Length | Species, data = iris)

densityplot()

#library(lattice)
densityplot(~Sepal.Length | Species, data = iris)

strip =

#library(lattice)
densityplot(~Sepal.Length | Species, data = iris,
            strip = strip.custom(bg="lightgrey"),
            par.strip.text=list(col="black", cex=.7, font=2))

splom()

#library(lattice)
splom(iris,
       type = c("p", "r", "smooth"),
       col.line = 2
      )

cloud() 3D

#library(lattice)
cloud(Sepal.Length~Petal.Length * Petal.Width,
       groups = Species,
       data = iris, 
       auto.key = TRUE,
       Scales = TRUE
      )

#library(lattice)
cloud(Sepal.Length~Petal.Length * Petal.Width | Species,
       groups = Species,
       data = iris, 
       auto.key = TRUE,
       Scales = TRUE
      )

#library(lattice)
cloud(y ~ x * test,
       groups = test,
       data = df, 
       auto.key = TRUE,
       Scales = TRUE
      )

levelplot()

#library(lattice)
levelplot(Sepal.Length ~ Petal.Length * Species,  data = iris)

#library(lattice)
levelplot(y ~ x * test,  data = df)

ggplot2

?ggplot2

Create Elegant Data Visualisations Using the Grammar of Graphics A system for ‘declaratively’ creating graphics, based on “The Grammar of Graphics”. You provide the data, tell ‘ggplot2’ how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.

ggplot2

#install.package("ggplot2")
library(ggplot2)
ggplot(data = iris, aes(x = Petal.Length, y = Sepal.Length)) +
       geom_point(aes(col = Species))

graph <- ggplot(data = iris, aes(x = Petal.Length, y = Sepal.Length))

graph + 
      geom_point() + 
      geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

graph + 
      geom_point() + 
      geom_smooth(se = T, method = "lm")

gg <- graph + 
        geom_point() + 
        geom_line(stat = "smooth", method = "lm", col = 2)
gg

CO2 Dataset

?CO2 The ‘CO2’ data frame has 84 rows and 5 columns of data from an experiment on the cold tolerance of the grass species_Echinochloa crus-galli_.

head(CO2)
str(CO2)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame':   84 obs. of  5 variables:
##  $ Plant    : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ...
##  $ Type     : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ...
##  $ conc     : num  95 175 250 350 500 675 1000 95 175 250 ...
##  $ uptake   : num  16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ...
##  - attr(*, "formula")=Class 'formula'  language uptake ~ conc | Plant
##   .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> 
##  - attr(*, "outer")=Class 'formula'  language ~Treatment * Type
##   .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> 
##  - attr(*, "labels")=List of 2
##   ..$ x: chr "Ambient carbon dioxide concentration"
##   ..$ y: chr "CO2 uptake rate"
##  - attr(*, "units")=List of 2
##   ..$ x: chr "(uL/L)"
##   ..$ y: chr "(umol/m^2 s)"

ggplot CO2 dataset

#library(ggplot2)
names(CO2)
## [1] "Plant"     "Type"      "Treatment" "conc"      "uptake"
gg1 <- ggplot(data = CO2, aes(x = conc , y = uptake))
         
gg1 + 
  geom_point()       

size = col =

gg1 + geom_point(size = 3, col = 2)

parameters in aes()

gg2  <- ggplot(data = CO2, aes(x = conc, 
                              y = uptake, 
                              shape = Treatment,
                              col = Type 
                              #size = Type
                             )) + 
                                  geom_point()
gg2

Zoom

gg2 + 
  coord_cartesian(xlim = c(500, 750), ylim = c(20, 40), expand = T) +
  ggtitle("Zoom CO2 x=500-750 y=20-40")

CO2$Plant
##  [1] Qn1 Qn1 Qn1 Qn1 Qn1 Qn1 Qn1 Qn2 Qn2 Qn2 Qn2 Qn2 Qn2 Qn2 Qn3 Qn3 Qn3
## [18] Qn3 Qn3 Qn3 Qn3 Qc1 Qc1 Qc1 Qc1 Qc1 Qc1 Qc1 Qc2 Qc2 Qc2 Qc2 Qc2 Qc2
## [35] Qc2 Qc3 Qc3 Qc3 Qc3 Qc3 Qc3 Qc3 Mn1 Mn1 Mn1 Mn1 Mn1 Mn1 Mn1 Mn2 Mn2
## [52] Mn2 Mn2 Mn2 Mn2 Mn2 Mn3 Mn3 Mn3 Mn3 Mn3 Mn3 Mn3 Mc1 Mc1 Mc1 Mc1 Mc1
## [69] Mc1 Mc1 Mc2 Mc2 Mc2 Mc2 Mc2 Mc2 Mc2 Mc3 Mc3 Mc3 Mc3 Mc3 Mc3 Mc3
## 12 Levels: Qn1 < Qn2 < Qn3 < Qc1 < Qc3 < Qc2 < Mn3 < Mn2 < Mn1 < ... < Mc1
gg3 <- ggplot(data = CO2, aes(x = uptake, y = conc, col = Treatment))+
         geom_point() +
         facet_wrap(~Plant)
 gg3      

plotly (package)

ggploty Convert ggplot2 to plotly

# in terminal deb sudo apt-get install libssl-dev
#install.packages("plotly", dependencies = TRUE)
library(plotly)
gg4 <- ggplotly(gg3)
#gg4

———————

End

———————

CC license: BY-SA