小编Tit*_*anz的帖子

反转默认比例梯度ggplot2

我是新手,我正在尝试设计热图。这是我的代码:

ggplot(gd, aes(Qcountry, Q6_1_Q6d), order = TRUE) +
  geom_tile(aes(fill = prob), colour = "white") +
  theme_minimal() +
  labs( y = "Main reason for mobility", x = "Country") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3)) +
  scale_fill_gradient(name = "(%)")
Run Code Online (Sandbox Code Playgroud)

这产生了一个完美的图表,我的问题是低水平是深蓝色,而较高的值是浅蓝色,这不直观。最常用的方法是使用rev()。但就我而言,我不知道该怎么做。因此,是否可以反转此默认比例这是传说

另一个问题是,有没有一种方法可以只用一种颜色创建比例渐变。我的意思是,scale_fill_gradient/scale_fill_gradientn 需要设置低色和高色(low = "", high = ""),我想将蓝色改为红色。

非常感谢你的支持。

reverse r heatmap ggplot2

7
推荐指数
2
解决办法
4843
查看次数

使用Word格式的rmarkdown报告回归表

尝试使用rmarkdown以Word格式报告回归表似乎是不可能的.经过几个小时和几个选项之后,没有人在我的情况下工作.我想lm使用markdown 报告模型并渲染到.doc文件.

方法1:

使用memisc包创建mtable对象并使用pander以下方式进行渲染:

> lm0 <- lm(hp ~ wt, mtcars)
> lm1 <- lm(qsec ~ hp, mtcars)
> lm2 <- lm(qsec ~ wt, mtcars)
> 
> library(memisc)
> 
> mt <- mtable(lm0, lm1, lm2)
> 
> pander::pander(mt)
Error in x[[i]] : subíndice fuera de  los límites
Además: Warning message:
In pander.default(mt) :
  No pander.method for "memisc_mtable", reverting to default.
Run Code Online (Sandbox Code Playgroud)

方法2:

创建一个html对象并包含使用includeHTML.到目前为止,这是我渴望输出的封闭方法.但是,该表只包含一个这样的列:

```{r}
stargazer::stargazer(lm0, lm1, lm2, type = …
Run Code Online (Sandbox Code Playgroud)

r knitr r-markdown

6
推荐指数
1
解决办法
1065
查看次数

在 Rmarkdown 中添加 HTML 文档左侧的目录

我正在尝试在 Rmarkdown 生成的 HTML 文档中添加目录。首先,我找到了这个答案,这似乎是一个简单的方法,在 HTML 文件中添加一个 CSS 文件,并使用以下代码:

#TOC {
  position: fixed;
  left: 0;
  top: 0;
  width: 200px;
  height: 100%;
  overflow:auto;
}
body {
  max-width: 800px;
  margin: auto;
  margin-left:210px;
  line-height: 20px;
}
Run Code Online (Sandbox Code Playgroud)

但我想从 Rmarkdown 修改 CSS。为了解决这个问题,我找到了另一个帖子,如何添加自定义 CSS 标签。但这并不正是我所寻找的,并且不知道如何以正确的方式将其与答案混合。

我的代码如下所示:

---
title: "R"
output: html_document
toc: yes
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r results="asis"}
cat("TOC {
  position: fixed;
  left: 0;
  top: 0;
  width: 200px;
  height: 100%;
  overflow:auto;
}
body {
  max-width: …
Run Code Online (Sandbox Code Playgroud)

html css r knitr r-markdown

5
推荐指数
1
解决办法
5197
查看次数

使用 ggplot 在 R 中绘制表情符号/表情符号

我正在尝试在 ggplot2 中用表情符号绘制一个情节。我想要的是显示表情符号而不是标签或像 geom 一样显示。Stack 中的这个问题与我的问题非常相似,但它也没有解决它,答案非常手动,我需要自动进行(我的真实数据框有 3000 行)。

只是为了在下面的图中澄清,我想显示表情符号而不是文本标签或条形图。但似乎不可能。

在此处输入图片说明

在尝试了不同的解决方案后,我堆积如山,所以任何帮助将不胜感激。我阅读了不同的方式来面对这个问题,比如这里这里这里

我最后一次尝试是这样的:

mult_format <- function() {
  function(x) emo::ji(x)
}
ggplot(foo, aes(name_emoji, n)) + 
  geom_bar(stat = "identity") +
  coord_flip() +
  scale_y_continuous(labels = mult_format())
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

我的数据看起来像这样,name_emoji 来自emo 包,我也可以将其更改为字节:

               name_emoji     n        emoji
                    <chr> <int>        <chr>
 1            closed book     1 "\U0001f4d5"
 2          confused face     7 "\U0001f615"
 3                  dizzy     20 "\U0001f4ab"
 4 face with tears of joy     1 "\U0001f602"
 5          flexed biceps     1 "\U0001f4aa"
 6             light …
Run Code Online (Sandbox Code Playgroud)

plot r emoticons ggplot2

5
推荐指数
2
解决办法
3003
查看次数

使用R中的tidyverse重新编码多个变量

可能是一个愚蠢的问题,我想在具有多个条件的tibble中重新编码多个变量.

数据示例:

library(tidyverse)
s <- matrix(sample(1:15, 20, replace = TRUE), ncol = 4)
s <- as_tibble(s)
Run Code Online (Sandbox Code Playgroud)

这给出了这样的东西:

# A tibble: 5 x 4
     V1    V2    V3    V4
  <int> <int> <int> <int>
1    11     2     5    14
2     5     4    15     5
3    13    15     2     5
4     7    13    15    11
5    11     5    12     3
Run Code Online (Sandbox Code Playgroud)

我想用这个条件重新编码V1,V2,V3,并使V4保持相等:如果值小于或等于5得1,如果值大于5但小于或等于10得到2,最后如果值超过10来3.

输出应如下所示:

# A tibble: 5 x 4
         V1    V2    V3    V4
      <int> <int> <int> <int>
    1    3     1     1     14
    2    1     1 …
Run Code Online (Sandbox Code Playgroud)

r dplyr recode tidyverse

2
推荐指数
1
解决办法
1676
查看次数

标签 统计

r ×5

ggplot2 ×2

knitr ×2

r-markdown ×2

css ×1

dplyr ×1

emoticons ×1

heatmap ×1

html ×1

plot ×1

recode ×1

reverse ×1

tidyverse ×1