一张图引发的R语言可视化学习之旅
library(imager)
library(magick)
## Linking to ImageMagick 6.9.9.14
## Enabled features: cairo, freetype, fftw, ghostscript, lcms, pango, rsvg, webp
## Disabled features: fontconfig, x11
# 读取图片
im <- image_read("fenxinboy.jpg")
#画它、画它、画它
plot(im)
###接下来用ggplot2画个抽象派的模拟图
# 加载所需要的包
library(readr)
library(dplyr)
library(forcats)
library(ggplot2)
library(patchwork)
# 读取并整理数据
data <-
read_csv("data.csv") %>%
mutate(
paint = fct_relevel(paint, c("black", "brown", "beige", "white", "black_2", "grey_2", "red", "blue_2", "beige_3", "blue", "beige_2", "grey"))
)
## Parsed with column specification:
## cols(
## paint = col_character(),
## x = col_double(),
## y = col_double()
## )
# 开始主体部分
p1 <-
ggplot(data) +
# 使用geom_bar来呈现人物主体
geom_bar(
mapping = aes(x = x, fill = paint),
width = 1
) +
# 设置不同的颜色来凸显人物
scale_fill_manual(
values = c("#24211a", "#593326","#e4a095", "#ffffff", "#24211a", "#93a9b6", "#fa0107", "#0b59b3", "#e4a095", "#0b59b3", "#e4a095","#93a9b6")
) +
# 制作方格
coord_equal() +
# coord_polar() +
# 加一个空白主题,坐标轴啥的都不要了
theme_bw() +
theme(
legend.position = "none",
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank()
)
# 自定义图例
legend_data <-
data.frame(
x = 0,
y = c(4, 6, 8, 10, 12),
label = c("M锅里的", "", "M男孩", "", "D碗里的")
)
# 使用ggplot2 画自定义的图例
p2 <-
ggplot(
data = legend_data,
mapping = aes(x = x, y = y)
) +
# 使用geom_tile创建填充框
geom_tile(
mapping = aes( fill = label),
width = 0.4, height = 0.4,
) +
# 使用geom_text将文字放置在图块旁边
geom_text(
mapping = aes(label = label),
size = 5,
nudge_x = 0.4,
nudge_y = 0,
hjust = 0,
vjust = 0.5
) +
scale_fill_manual(
values = c("#ffffff", "#93a9b6", "#fa0107", "#0b59b3")
) +
scale_x_continuous(limits = c(-0.5,3)) +
scale_y_continuous(limits = c(1, 15)) +
coord_fixed(ratio = 0.8) +
# 加一个主题
theme_void() +
theme(
legend.position = "none"
)
# 使用 patchwork 包来组合图片主体和自定义图例
p <- p1 + p2
p
# 保存画出来的图片
ggsave(filename = "组合分心图.png", plot = p, width = 16, height = 9)
# 最后知道真相的我眼泪掉了下来、、、、、
library(imager)
library(magick)
inn <-image_read("fenxinboystory.jpg")
plot(inn)
###心里很乱,像个红色的毛线球,乱糟糟的、、、想静静了
感谢Ashten Anthony的分享与指导,大家多多支持,以后会陆续更新!
大家有想要学习的内容也可以留言,小编会持续关注。