# 需要提前安装好:install.packages("lattice")
library(lattice)
# 需要提前安装好:install.packages("ggplot2")
library(ggplot2)
# 需要提前安装好:install.packages("ggiraphExtra")
library(ggiraphExtra)
mydata <- mtcars[, c(1, 9)]
mydata$names <- row.names(mydata)
mydata$am <- factor(mydata$am, labels = c("automatic", "manual"))
str(mydata); head(mydata, 10)
dotchart(mydata$mpg, main = "Motor Trend Car Road Tests")
dotchart(mydata$mpg,
pch = 19, # 形状
col = hcl.colors(32, palette = "Set2"), # 颜色
pt.cex = 1.5, # 大小
labels = mydata$names, # y轴上,组别标签
frame.plot = TRUE, # 图片周围方框
main = "Motor Trend Car Road Tests",
xlab = "Miles/(US) gallon",
ylab = "Names of automobiles")
dotchart(mydata$mpg,
xlim = c(10,40), # x轴范围
bg = "skyblue", # 底色
pch = 22,
pt.cex = 1.2,
labels = mydata$names, # y轴上亚组名
groups = mydata$am, # 分组
gcolor = c("steelblue", "tomato"),
main = "Motor Trend Car Road Tests", # 标题名
xlab = "Miles/(US) gallon", # x轴标题
ylab = "Names of automobiles") # y轴标题
dotplot(names ~ mpg|am, data = mydata, groups = am,
auto.key = list(space = "right", points = TRUE, lines = FALSE), # 修改legend
main = "Motor Trend Car Road Tests",
xlab = "Miles/(US) gallon",
ylab = "Names of automobiles")
ggplot(mydata, aes(x = names, y = mpg, colour = am)) +
geom_point(size = 3) + # 设置点的大小
scale_colour_manual(values = c("steelblue", "tomato")) +
coord_flip() + # 翻转图片
theme_bw() + # 设置主题风格
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(linetype = 2)) + # 修饰grids
labs(title = "Motor Trend Car Road Tests",
x = "Names of automobiles",
y = "Miles/(US) gallon",
colour = "Transmission")
ggCLE(data = mydata, aes(x = mpg, y = names, colour = names)) +
labs(title = "Motor Trend Car Road Tests",
x = "Miles/(US) gallon")
增加一个分类变量am:
ggCLE(data = mydata, aes(x = mpg, y = names, colour = am, facet = am)) +
scale_colour_manual(values = c("#009E73", "#D55E00")) +
labs(title = "Motor Trend Car Road Tests",
y = "Names of automobiles",
x = "Miles/(US) gallon")