小编Gre*_*ell的帖子

如何将因子水平设置为它们在数据框中出现的顺序?

我想用ggplot创建一个热图,但我想按观察次数对y轴进行排序.我按N列对数据帧进行排序,并将观察次数添加到组名称中,以使其显示在轴标签中.当我绘制数据时,它会根据组名重新排序.有没有办法根据它们在数据框中出现的顺序设置因子水平?

一些数据:

library(dplyr)
library(tidyr)
library(ggplot2)

school <- c("School A", "SChool B", "School C", "School D", "School E", "School F")
N <- c(25,28,12,22,30,25)
var1 <- c(1,0,1,1,0,1)
var2 <- c(0,0,0,1,0,1)
var3 <- c(0,1,0,1,1,1)

df <- tbl_df (data.frame (school, N, var1, var2, var3))

df <- arrange (df, N) %>%
  gather (variable, value, var1:var3)

df$school <- paste0 (df$school, " (", df$N, ")")

df <- select (df, school, variable, value)

ggplot(df, aes(variable, school)) + geom_tile(aes(fill = value), colour = "white") + 
  scale_fill_gradient(low = "white",high = …
Run Code Online (Sandbox Code Playgroud)

r

10
推荐指数
2
解决办法
3620
查看次数

如何在Markdown中为PDF文档添加图像作为页眉/页脚

我想将图像添加为页眉和页脚(与我的组织视觉识别一致)到PDF报告.我已经设法在Sweave中做到了,但我也希望能够使用Markdown来做到这一点.我可以使用插入图像![footer](H:/R/Footer.pdf)但是当我尝试使用壁纸包时(就像我在Sweave中一样)我收到错误.

---
title: "2014 Report"
author: "My Name"
date: "Monday, October 06, 2014"
output:
  pdf_document:
    number_sections: yes
    toc: yes
fontsize: 10pt
header-includes:
    - \usepackage{wallpaper}
abstract: This is my abstract
---
\LRCornerWallPaper{0.99}{![footer](H:/R/Footer.pdf)} 
Run Code Online (Sandbox Code Playgroud)

Markdown有办法,还是我需要坚持Sweave?

markdown r knitr

9
推荐指数
1
解决办法
4544
查看次数

在ggplot中调整geom_bar(position ="dodge")

我想在ggplot中创建一个2变量条形图,其中一个度量部分隐藏在另一个之后.我可以使用Series Overlap在Excel中执行此操作并获得此结果.

使用geom_bar(position ="dodge")将两个条并排放置.有没有办法调整这个?

一些代码:

library (ggplot2)
library(reshape2)
x <- c(19, 18, 21, 19)
y <- c(17, 16, 18, 19)
z <- c("a", "b", "c", "d")

df <- melt (data.frame (x,y,z))

ggplot (df, aes(x=z, y=value, fill=variable)) + geom_bar (stat="identity", position ="dodge")
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

用李克特软件包绘制百分比-分组时不起作用

我已经使用Likert包创建了一些图表,但是当我按组创建图时,plot.percents = TRUE不会为我提供每个响应类别的标签。plot.percents.high = TRUE和plot.percents.low = TRUE为我提供了组合百分比,但是我希望所有响应类别都使用它。它可以很好地与未分组的数据一起使用。我使用的代码是:

做一些数据

library(likert)
library (reshape)

Group <- c("Group 1", "Group 1", "Group 1", "Group 1", "Group 1", "Group 1", "Group 1", "Group 2", "Group 2", "Group 2", "Group 2", "Group 2",
           "Group 2","Group 2", "Group 3", "Group 3", "Group 3", "Group 3","Group 3","Group 3","Group 3")

Var1 <- c("Agree", "Agree", "Strongly agree", "Agree", "Strongly disagree", "Agree","Strongly agree", "Disagree", "Strongly agree",
          "Strongly agree", "Agree", "Disagree", "Agree", "Strongly disagree", "Agree", "Agree", "Agree", "Disagree", "Strongly agree",
          "Strongly disagree", …
Run Code Online (Sandbox Code Playgroud)

r

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

ggplot 自定义字体在四开中不起作用

当我使用自定义字体渲染 ggplot 时,它可以在 IDE 中工作,但是在 Quarto 和 RMarkdown 中我不断收到此错误:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
Calls: .main ... drawDetails -> drawDetails.text -> grid.Call.graphics
Run Code Online (Sandbox Code Playgroud)

这些字体都安装在我的系统上,但 Quarto 或 RMakrdown 无法识别。
例如,这是我的脚本:

---
title: "TEST"
format: pdf
editor: visual
---

This plot works:
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(tidyverse)

mtcars |>
  count (cyl) |>  
  ggplot (aes (x = cyl, y = n)) + 
  geom_col() + 
  labs (title = "Plot 1") + 
  geom_text (aes (label = n), 
             vjust = -1)
``` …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 r-markdown quarto

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

将标题添加到xtable

我不确定这是一个R问题还是一个Sweave问题.我在一张简单的桌子(3年级和6年级)中从2个时间点看学生的标记.我创建了桌子并打印,但是我想添加标签,以便人们知道哪个标记来自哪个等级.

我的Sweave代码是:

<<MakeData,results='asis'>>=
library(xtable)
Grade3 <- c("A","B","B","A","B","C","C","D","A","B","C","C","C","D","B","B","D","C","C","D")
Grade6 <- c("A","A","A","B","B","B","B","B","C","C","A","C","C","C","D","D","D","D","D","D")
Cohort <- table(Grade3,Grade6)
print(xtable(Cohort))
@
Run Code Online (Sandbox Code Playgroud)

我得到一个很好的表计数,但行和列都有相同的表示法.如何添加标签以使其更清晰?

r sweave

4
推荐指数
1
解决办法
1万
查看次数

使用R Markdown在HTML演示文稿中进行超链接

我希望我已经正确标记了这一点 - 我想在R Studio中使用Markdown创建一个HTML演示文稿.我想要做的是创建超链接,点击时将跳转到指定页面,而不是大多数演示文稿的直线性进展.例如:

---
title: "Presentation"
output: ioslides_presentation
---

## Slide 1

This is the first slide. I'd like to be able to insert hyperlinks to a different page within the slide. For example:

[Slide 2](hyperlink to slide 2) - clicking this would jump to slide 2

[Slide 3](hyperlink to slide 3) - clicking this would jump to slide 3

[Slide 4](hyperlink to slide 4) - clicking this would jump to slide 4

## Slide 2

Text for slide …
Run Code Online (Sandbox Code Playgroud)

html markdown rstudio knitr

3
推荐指数
1
解决办法
1642
查看次数

使用gridExtra中的tableGrob在表格中包装文本

我有一系列调查问题,并且正在报告学校,学校家庭和整个董事会的回复.有些问题很长 - 我想要做的是在列结束时自动换行.现在我必须手动添加换行符.这甚至可能吗?我正在使用R版本3.2.1和gridExtra_2.0.0.

一些代码:

library(gridExtra)
library(grid)
library(ggplot2)

key <- c("I feel close to other parents with children the same age", "I am comfortable asking for advice about parenting", "I take time out to take care of my own health and well?being")
SCH <- c(20,30,40)
FOS <- c(25,35,56)
BOARD <- c(32,44,58)

d <- data.frame(key, SCH, FOS, BOARD)

row.names(d) <- d[,1]
d[,1] <- NULL

g <- tableGrob(d)

g$widths <- unit (c(0.4, 0.1,0.1,0.1),"npc")
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)

r gridextra

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

标签 统计

r ×7

ggplot2 ×2

knitr ×2

markdown ×2

gridextra ×1

html ×1

quarto ×1

r-markdown ×1

rstudio ×1

sweave ×1