R繪圖_ggplot2繪制qplot

ggplot2在線學(xué)習(xí):STHDA :Statistical tools for high-throughput data analysis.
ggplot2使用說(shuō)明:https://ggplot2.tidyverse.org/reference/

  • 火狐截圖_2020-02-11T08-36-22.554Z.png

qplot

英文:http://www.sthda.com/english/wiki/qplot-quick-plot-with-ggplot2-r-software-and-data-visualization

根據(jù)說(shuō)明文檔,運(yùn)行代碼……

#The data set mtcars is used in the examples below:
data(mtcars)
head(mtcars)
df <- mtcars[, c("mpg","cyl","wt")]
head(df)

#The plot can be created using data from either numeric vectors or a data frame:
# Use data from numeric vectors
x <- 1:10; y = x*x
# Basic plot
qplot(x,y)
# Add line
qplot(x, y, geom=c("point", "line"))
# Use data from a data frame
qplot(mpg, wt, data = mtcars)

#Scatter plots with smoothed line
# Smoothing
qplot(mpg, wt, data = mtcars, geom = c("point", "smooth"))

#The argument color is used to tell R that we want to color the points by groups:
# Linear fits by group
qplot(mpg, wt, data = mtcars, color = factor(cyl), geom = c("point","smooth"))
  
# Change the color by a continuous numeric variable
qplot(mpg, wt, data = mtcars, color = cyl)
# Change the color by groups (factor)
qplot(mpg, wt, data = mtcars, color = factor(cyl))
qplot(mpg, wt, data = mtcars, color = factor(cyl), geom = c("point","line"))

# Change the size of points according to 
# the values of a continuous variable
qplot(mpg, wt, data = mtcars, size = mpg)
# Change point shapes by groups
qplot(mpg, wt, data = mtcars, shape = factor(cyl))

#Scatter plot with texts
qplot(mpg, wt, data = mtcars, label = rownames(mtcars), 
      geom = c("point", "text"),)

#hjust 和 vjust 的值僅定義在0和1之間:#0表示左對(duì)齊 #1表示右對(duì)齊
qplot(mpg, wt, data = mtcars, label = rownames(mtcars), 
      geom=c("point", "text"),
      hjust=0, vjust=0)

#Box plot, dot plot and violin plot
head(PlantGrowth)
x <- 1
y <- rnorm(100)
qplot(x, y, geom = "boxplot")

qplot(group, weight, data = PlantGrowth, geom = "boxplot")
#Dot plot
qplot(group, weight, data = PlantGrowth)
qplot(group, weight, data = PlantGrowth, geom = "dotplot", stackdir = "center", binaxis = "y")
#violin plot
qplot(group, weight, data = PlantGrowth, geom = "violin", trim = FALSE)
#add jitter and change fill color by group
qplot(group, weight, data = PlantGrowth, geom = c("boxplot", "jitter"), fill = group)
#dot plot
qplot(group, weight, data = PlantGrowth, 
      geom = "dotplot", stackdir = "center", binaxis = "y",
      color = group, fill = group)

#The histogram and density plots are used to display the distribution of data.
set.seed(1234)
mydata <- data.frame(
  sex = factor(rep(c("F","M"), each = 200)),
  weight = c(rnorm(200, 55), rnorm(200, 58))
)
#notice the difference : rep(c("1","2"),each = 5) / rep(c("1","2"), 5)
head(mydata)
#basic histogram 
#the histogram and density plots are used to display the distributin of data
qplot(weight, data = mydata, geom = "histogram")
qplot(weight, data = mydata, geom = "histogram", color = sex)
qplot(weight, data = mydata, geom = "histogram", fill = sex)

#basic density plot
qplot(weight, data = mydata, geom = "density")
#change the density plot line color by group(sex) \ change the line type
qplot(weight, data = mydata, geom = "density", color = sex, linetype = sex)

#main title and axis labels
qplot(weight, data = mydata, geom = "density",
      xlab = "Weight(kg)", ylab = "Density",
      main = "Density plot of Weight(kg)")
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容