R语言从拍摄照片生成色卡用于统计图

ok.png

ex_2021-04-29_02-10-25.png
ex_2021-04-29_02-13-55.png
图片1.jpg
微信图片_20210429013555.jpg
# Thu Apr 29 01:37:23 2021 -

# 字符编码:UTF-8
# R 版本:R x64 4.0.5 for window 10
# [email protected]
# 个人笔记不负责任,拎了个梨
.rs.restartR()
require(imgpalr)
require(magick)
require(billboarder)
require(magickGUI)
rm(list = ls());gc()

myimg <- image_read('微信图片_20210429013555.jpg')
myimg
myimg.ok <- interactive_crop(myimg)
myimg.ok
image_write(myimg.ok,path = '~/ex1.jpg')

n <- 10 # 颜色数量
image_pal('~/ex1.jpg', n = n, type = "div",
          saturation = c(0.75, 1), brightness = c(0.75, 1), plot = TRUE)

myco <- image_pal('~/ex1.jpg', n = n, type = "div",
          saturation = c(0.75, 1), brightness = c(0.75, 1), plot = TRUE)

myco
# Thu Apr 29 01:38:09 2021 ------------------------------


require(billboarder)

#随机数模拟坐标散点:
mydt <- data.frame(
  lat = runif(10, 30, 35),
  lon = runif(10, 116.3, 121.9) ,
  size = round(runif(10, 5, 50), 2) ,
  type = sample(LETTERS[1:5], 10, replace =TRUE),
  co = substr(rainbow(10), 1, 7))
head(mydt)# lat lon size type co

billboarder() %>%
  bb_piechart(data = mydt) %>%
  bb_color(palette = myco)

# Tue Apr 27 23:50:21 2021 --end
# Thu Apr 29 02:14:29 2021 ----gg--------------------------
require(ggplot2)
library(showtext)
showtext_auto() # 避免字体错乱

ggplot(data = mydt)+
  geom_point(aes(x = lon,y = lat,color=co),
             show.legend= T,size=8)+
             geom_text(aes(y = lat,x = lon,
                           label=co))+
  scale_color_manual(values=myco,
                     label = myco %>% as.character() # 图例名 连续变量
                     )+
  theme(
    legend.text= element_text(
      family = "xingkai",  #字体
      face = "bold", #字型(黑体、斜体etc)
      size = 10, #字号
      colour = myco)) +#颜色
   labs(
     # breaks ='aaa',
    title = "从拍摄照片生成色卡用于统计图", #标题名
    x = "",    #横轴名
    y = "",    #纵轴名
    col = "色值"   #图例名
# ,fill=myco
  )
# Thu Apr 29 02:42:39 2021 --end
# Thu Apr 29 02:44:57 2021 ----添加静态照片--------------------------
require(grid)
vp <- viewport(x = 0.6, y = 0.3, width = 0.5, height = 0.5)
grid.raster(myimg, vp = vp)
# Thu Apr 29 02:47:35 2021 --end
dev.copy(png, "ok.png",width=1080,height=1080);dev.off()

你可能感兴趣的:(R语言从拍摄照片生成色卡用于统计图)