R 到 pdf,代码和页面边缘的绘图

R to pdf, code and plots off the edge of the page

提问人:Clauric 提问时间:11/16/2023 最后编辑:Clauric 更新时间:11/16/2023 访问量:29

问:

我试过在网上寻找,但我似乎找不到任何可行的答案。

我有一个 rmd 笔记本,上面写满了情节。当它被运行和保存时,一切都可以正常工作。当我将其编织成html时,一切都正确呈现。

但是,当我将其编织成pdf时,会发生许多事情:

  1. 代码不会换行在代码框内,而是在页面的右侧运行。
  2. 代码的输出从页面的输出运行,而不是换行。
  3. 第一个图没问题,但所有后续图都逐渐向右偏移(因此图 2 稍微靠右,图 3 位于中心右侧,依此类推)。
  4. 应位于代码框上方的标头位于绘图下方,即使它们不在代码框中也是如此。

我假设所有这些问题都是相关的,但是,我似乎找不到解决它们的方法。我在 rmd 开头的代码块是:

---
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: 
  pdf_document:
    keep_tex: true

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

...

library(ggplot2)
library(dplyr)

任何关于如何解决的建议将不胜感激。

编辑:

官方代码:

各种小提琴情节

大小(以字节为单位)与价格

p <- ggplot(apple_df, 
            aes(price, size_bytes), 
            )

p + geom_violin() + coord_flip() + labs(
    x = "Price in USD",
    y = "Size in bytes", 
    title = "Violin plot - size in bytes vs price"
)
  

输出 pdf 格式:

enter image description here enter image description here

官方代码:

corr_1 <- cor(apple_df$size_bytes, apple_df$user_rating)
if (corr_1 < -0.75) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a very strong negative correlation between app size and customer rating"))
} else if (corr_1 < -0.5) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a strong negative correlation between app size and customer rating"))
} else if (corr_1 < -0.25) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a weak negative correlation between app size and customer rating"))
} else if (corr_1 < -0.1) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a very weak negative correlation between app size and customer rating"))
} else if (corr_1 >= -0.1 && corr_1 <= 0.1) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is no correlation between app size and customer rating"))
} else if (corr_1 > 0.75) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a very strong positive correlation between app size and customer rating"))
} else if (corr_1 > 0.5) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ". This suggests there is a strong postive correlation between app size and customer rating"))
} else if (corr_1 > 0.25) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a weak postive correlation between app size and customer rating"))
} else {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a very weak positive correlation between app size and customer rating"))
}

以 PDF 格式输出:enter image description here

第二次编辑:

示例代码块。

问题 4:使用 groupby 查找每种类型的应用大小总和。

改编自:https://sparkbyexamples.com/r-programming/r-group-by-function-from-dplyr/#:~:text=Dplyr%20group_by%20Function%20Example,frame%20as%20a%20first%20argument

group_by_table <- apple_df %>% group_by(apple_df$prime_genre)
agg_table <- group_by_table %>% summarise(sum(size_bytes))
agg_df <- agg_table %>% as.data.frame()
colnames(agg_df) <- c("App genres", "Sum of app sizes (in bytes)")
agg_df

问题 5,i:客户对大型应用(以字节为单位的较大大小)的评分是否更高 - 使用相关性?

corr_1 <- cor(apple_df$size_bytes, apple_df$user_rating)
if (corr_1 < -0.75) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a very strong negative correlation between app size and customer rating"))
} else if (corr_1 < -0.5) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a strong negative correlation between app size and customer rating"))
} else if (corr_1 < -0.25) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a weak negative correlation between app size and customer rating"))
} else if (corr_1 < -0.1) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a very weak negative correlation between app size and customer rating"))
} else if (corr_1 >= -0.1 && corr_1 <= 0.1) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is no correlation between app size and customer rating"))
} else if (corr_1 > 0.75) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a very strong positive correlation between app size and customer rating"))
} else if (corr_1 > 0.5) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ". This suggests there is a strong postive correlation between app size and customer rating"))
} else if (corr_1 > 0.25) {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a weak postive correlation between app size and customer rating"))
} else {
  cat(paste("The correlation between app size and ratings is", round(corr_1, digits = 2), ".\nThis suggests there is a very weak positive correlation between app size and customer rating"))
}

问题 5、ii:评分较高的应用是否评分更高 - 使用相关性?

corr_2 <- cor(apple_df$rating_count_tot, apple_df$user_rating)
if (corr_2 < -0.75) {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ".\nThis suggests there is a very strong negative correlation between the number of ratings submitted and customer rating."))
} else if (corr_2 < -0.5) {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ".\nThis suggests there is a strong negative correlation between the number of ratings submitted and customer rating."))
} else if (corr_2 < -0.25) {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ".\nThis suggests there is a weak negative correlation between the number of ratings submitted and customer rating."))
} else if (corr_2 < -0.1) {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ".\nThis suggests there is a very weak negative correlation between the number of ratings submitted and customer rating."))
} else if (corr_2 >= -0.1 && corr_2 <= 0.1) {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ".\nThis suggests there is no correlation between the number of ratings submitted and customer rating."))
} else if (corr_2 > 0.75) {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ".\nThis suggests there is a very strong positive correlation between the number of ratings submitted and customer rating."))
} else if (corr_2 > 0.5) {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ".\nThis suggests there is a strong postive correlation between the number of ratings submitted and customer rating."))
} else if (corr_2 > 0.25) {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ". This suggests there is a weak postive correlation between the number of ratings submitted and customer rating."))
} else {
  cat(paste("The correlation between app size and ratings is", round(corr_2, digits = 2), ".\nThis suggests there is a very weak positive correlation between the number of ratings submitted and customer rating."))
}

版本: RStudio:202.09.1 内部版本 494RStudio: 202.09.1 build 494 中号:4.3.2 dplyr:1.1.3 ggplot2:3.4.4

PDF R-降价

评论


答: 暂无答案