计算机@linux_python_R 技术帖R

ggtext: 文本支持 HTML and Markdown (

2019-08-11  本文已影响65人  热衷组培的二货潜

首先我是看到 Twitter 的图才对这个包特别有兴趣的:https://twitter.com/hrbrmstr/status/1159893212640763910

作者也很 nice 的贴了他的核心代码,以及他的参考来源 ggtext, 然后我就来搬运了。哈哈哈


# 现在应该是可以直接安装了,不会报错,我也不知道咋回事。可能休息的这一天作者又更新了。

首先安装 ggtext

> devtools::install_github("clauswilke/ggtext")
Downloading GitHub repo clauswilke/ggtext@master
Error in utils::download.file(url, path, method = method, quiet = quiet,  : 
  cannot open URL 'https://api.github.com/repos/clauswilke/ggtext/tarball/master'

我。。。 反复试了好几遍都是 Error Error Error Error Error Error Error

然后经过我的日常操作:

复制错误 → 打开谷歌 → 粘贴错误

发现了解决办法,好吧关于 TLS 我也不知道是什么,跟着操作就行了。( 题外话:咱们这一行真的一定要能上谷歌,能给你省很多时间,实在没钱没方法你去这里也行 萌搜 为小众而搜)

打开 W10小娜 → 搜索 interenet 选项, 点击 → 找到 高级 选项 → 上选项 TLS 1.2 就好了

再来安装包

> devtools::install_github("clauswilke/ggtext")

然后提示我要安装 RTools 35.exe


选择 Yes,然后又报错了,说
> devtools::install_github("clauswilke/ggtext")
Downloading GitHub repo clauswilke/ggtext@master
Downloading GitHub repo clauswilke/gridtext@master
trying URL 'https://cran.rstudio.com/bin/windows/Rtools/Rtools35.exe'
Content type 'application/x-msdownload' length 108622512 bytes (103.6 MB)
downloaded 4.0 MB

Error: Failed to install 'ggtext' from GitHub:
  Failed to install 'gridtext' from GitHub:
  Could not find tools necessary to compile a package
In addition: Warning message:
In utils::download.file("https://cran.rstudio.com/bin/windows/Rtools/Rtools35.exe",  :
  downloaded length 4198400 != reported length 108622512

好吧,既然这里又报错了,经过查询发现我的

> system('g++ -v')
Using built-in specs.
COLLECT_GCC=C:\RBUILD~1\3.4\mingw_32\bin\G__~1.EXE
COLLECT_LTO_WRAPPER=C:/RBUILD~1/3.4/mingw_32/bin/../libexec/gcc/i686-w64-mingw32/4.9.3/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: ../../../src/gcc-4.9.3/configure --host=i686-w64-mingw32 --build=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/home/Jeroen/mingw-gcc-4.9.3/i686-493-posix-dwarf-rt_v3-s/mingw32 --with-gxx-include-dir=/mingw32/i686-w64-mingw32/include/c++ --enable-static --disable-shared --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-sjlj-exceptions --with-dwarf2 --disable-isl-version-check --disable-cloog-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=i686 --with-tune=generic --with-libiconv --with-system-zlib --with-gmp=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --with-mpfr=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --with-mpc=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --with-isl=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --with-cloog=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static --enable-cloog-backend=isl --with-pkgversion='i686-posix-dwarf, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/home/Jeroen/mingw-gcc-4.9.3/i686-493-posix-dwarf-rt_v3-s/mingw32/opt/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-zlib-static/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/home/Jeroen/mingw-gcc-4.9.3/i686-493-posix-dwarf-rt_v3-s/mingw32/opt/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-zlib-static/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/home/Jeroen/mingw-gcc-4.9.3/i686-493-posix-dwarf-rt_v3-s/mingw32/opt/lib -L/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-zlib-static/lib -L/home/Jeroen/mingw-gcc-4.9.3/prerequisites/i686-w64-mingw32-static/lib -Wl,--large-address-aware'
Thread model: posix
gcc version 4.9.3 (i686-posix-dwarf, Built by MinGW-W64 project) 
[1] 0

# 这里可以看到我的编译器是 RTools 34.exe
> system('where make')
C:\RBuildTools\3.4\bin\make.exe
c:\Rtools\bin\make.exe
[1] 0

w10 安装 RTools 35.exe, 下载链接:RTools

> library(pkgbuild)
> devtools::install_github("clauswilke/ggtext")
Error: Failed to install 'unknown package' from GitHub:
  HTTP error 403.
  API rate limit exceeded for 211.69.128.53. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

  Rate limit remaining: 0/60
  Rate limit reset at: 2019-08-10 07:39:03 UTC

  To increase your GitHub API rate limit
  - Use `usethis::browse_github_pat()` to create a Personal Access Token.
  - Use `usethis::edit_r_environ()` and add the token as `GITHUB_PAT`.

解决方案: 我的源是 清华源 炸了,换一个源就好了

> options("repos")
$repos
                                        CRAN 
"https://mirrors.tuna.tsinghua.edu.cn/CRAN/" 

还没安装好,我心态快炸了 , 谁安装好了,麻烦教教我,十块钱小红包
我特么。。。玩了一天回来就可以了。


好吧,先码

前面的报错都不用管了, 直接安装,也算是一次排错对自己的锻炼吧。


ggtext 包定义了一个新的主题元素 element_markdown() ,类似于 element_text(),但将提供的文本呈现为markdown/htmlelement_markdown()禁用了自动换行。要开始新行,使用 <br> 标签或在行尾之前添加两个空格。

# devtools::install_github("clauswilke/ggtext")
library(ggplot2)
library(ggtext)

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(
    name = NULL,
    values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
    labels = c(
      setosa = "<i style='color:#0072B2'>I. setosa</i>",
      virginica = "<i style='color:#009E73'>I. virginica</i>",
      versicolor = "<i style='color:#D55E00'>I. versicolor</i>")
  ) +
  labs(
    title = "**Fisher's *Iris* dataset**  
    <span style='font-size:11'>Sepal width vs. sepal length for three *Iris*
    species</span>",
    x = "Sepal length (cm)", y = "Sepal width (cm)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_markdown(lineheight = 1.1),
    legend.text = element_markdown(size = 11)
  )

支持使用 <img> 作为标签,并且可以使用它,例如,将图像用作坐标轴标签

labels <- c(
  setosa = "<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Iris_setosa.JPG/180px-Iris_setosa.JPG'
    width='100' /><br>*I. setosa*",
  virginica = "<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Iris_virginica_-_NRCS.jpg/320px-Iris_virginica_-_NRCS.jpg'
    width='100' /><br>*I. virginica*",
  versicolor = "<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/20140427Iris_versicolor1.jpg/320px-20140427Iris_versicolor1.jpg'
    width='100' /><br>*I. versicolor*"
)
ggplot(iris, aes(Species, Sepal.Width)) +
  geom_boxplot() +
  scale_x_discrete(
    name = NULL,
    labels = labels
  ) +
  theme(
    axis.text.x = element_markdown(color = "black", size = 11)
  )

geom_rich_text() 函数提供了 markdown/html 标签. 与 geom_label() 不同的是, 标签能够旋转

df <- data.frame(
  label = c(
    "Some text **in bold.**",
    "Linebreaks<br>Linebreaks<br>Linebreaks",
    "*x*<sup>2</sup> + 5*x* + *C*<sub>*i*</sub>",
    "Some <span style='color:blue'>blue text **in bold.**</span><br>And *italics text.*<br>
    And some <span style='font-size:18; color:black'>large</span> text."
  ),
  x = c(.2, .1, .5, .9),
  y = c(.8, .4, .1, .5),
  hjust = c(0.5, 0, 0, 1),
  vjust = c(0.5, 1, 0, 0.5),
  angle = c(0, 0, 45, -45),
  color = c("black", "blue", "black", "red"),
  fill = c("cornsilk", "white", "lightblue1", "white")
)
ggplot(df) +
  aes(
    x, y, label = label, angle = angle, color = color, fill = fill,
    hjust = hjust, vjust = vjust
  ) +
  geom_rich_text() +
  geom_point(color = "black", size = 2) +
  scale_color_identity() +
  scale_fill_identity() +
  xlim(0, 1) + ylim(0, 1)

去除标签的框架或背景

ggplot(df) +
  aes(
    x, y, label = label, angle = angle, color = color,
    hjust = hjust, vjust = vjust
  ) +
  geom_rich_text(
    fill = NA, label.color = NA, # remove background and outline
    label.padding = grid::unit(rep(0, 4), "pt") # remove padding
  ) +
  geom_point(color = "black", size = 2) +
  scale_color_identity() +
  xlim(0, 1) + ylim(0, 1)
上一篇 下一篇

猜你喜欢

热点阅读