小编tas*_*nes的帖子

如何使用 ggplot2 在 R 中制作不连续轴?

我有一个dat包含两列 1)Month和 2)的数据框 ( ) Value。我想强调一下,箱线图中的 x 轴不是连续的,方法是用 x 轴上的两条有角度的线(在有角度的线之间是空的)中断 x 轴。

示例数据和箱线图

library(ggplot2)
set.seed(321)
dat <- data.frame(matrix(ncol = 2, nrow = 18))
x <- c("Month", "Value")
colnames(dat) <- x
dat$Month <- rep(c(1,2,3,10,11,12),3)
dat$Value <- rnorm(18,20,2)

ggplot(data = dat, aes(x = factor(Month), y = Value)) +
  geom_boxplot() +
  labs(x = "Month") +
  theme_bw() +
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black"),
        axis.text.y = element_text(size = 14, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

将二级标题添加到facet_wraps

我有一个数据框,由分布在两个位置(即和)的八个站点(即ABC... )组成。我已经为每个站点制作了一个数字,但是,我想添加一个额外的列标题来表示该站点的位置。我怎样才能做到这一点?HNorthSouthfacet_wrap()

示例数据

library(ggplot2)
library(dplyr)

set.seed(123)

df <- data.frame(matrix(ncol = 4, nrow = 24))
colnames(df)[1:4] <- c('location','site','x','y')
df$location <- rep(c('North','North','North','South','South','South'),4)
df$site <- c('A','A','A','E','E','E','B','B','B','F','F','F',
             'C','C','C','G','G','G','D','D','D','H','H','H')
df$x <- rep(seq(0,12,4),6)
df$y <- rnorm(24,50,20)
df
Run Code Online (Sandbox Code Playgroud)

示例图(缺少辅助标头)

df %>%
  mutate(across(site, factor, levels = c('A','B','E','F',
                                         'C','D','G','H'))) %>%
  ggplot(aes(x = x, y = y)) +
  geom_point() +
  geom_line() +
  scale_x_continuous(breaks = seq(0,12,3),
                     limits = c(0,12)) +
  scale_y_continuous(breaks = seq(0,max(df$y),5)) +
  theme_bw() +
  facet_wrap(~site, nrow = 2)
Run Code Online (Sandbox Code Playgroud)

这是一个类似的问题( …

r ggplot2

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

在R中,如果有多个TRUE答案,则选择第一个TRUE答案

我有一个数据框,代表一条河流的两年每日温度时间序列。对于这条河,我想知道一年中的哪一天(doy):

  1. 温度持续大于或等于10度
  • 持续是指在一年中的最高气温之后(例如秋季或冬季)之前不再有低于 10 的气温下降
  1. 温度持续小于或等于10度
  • 持续是指直到下一年不再有超过 10 的峰值

当我尝试计算 2 时,我遇到了错误,因为代码有多个TRUE答案可供选择。我想知道TRUE如果有多个TRUE答案,如何使代码与第一个答案一致。

示例数据集

library(ggplot2)
library(lubridate)
library(dplyr)
library(dataRetrieval)

siteNumber <- "01417500"
parameterCd <- "00010" # water temperature
statCd <- "00003" # mean
startDate <- "2015-01-01"
endDate <- "2016-12-31"

dat <- readNWISdv(siteNumber, parameterCd, startDate, endDate, statCd=statCd)
dat <- dat[,c(2:4)]
colnames(dat)[3] <- "temperature"

# Visually inspect the time series
ggplot(data = dat, aes(x = Date, y = temperature)) +
  geom_point() +
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

1 …

r

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

如何将网站中的表格数据抓取到 R 中?

我正在开发一个项目,目前需要我每天访问该网站(https://returntogrounds.virginia.edu/covid-tracker),并手动将每个新的日期dateUVA positive cases值添加到数据框中。有没有我可以在 R 中运行的代码来创建一个数据框date,而UVA positive cases不是我每天都必须手动添加新数据?我看到这里有一个类似的问题,但这是针对我不熟悉的 python 的。

r dataframe web-scraping tableau-api

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

在 R 中,如何调整 stat_pvalue_manual() 中的有效位数?

该函数stat_pvalue_manual()将使用 向箱线图添加 p 值ggboxplot。然而,打印的 p 值有时有很多位长。我想将小数位数限制为 3 位。我该怎么做?

从下图中您将看到 versicolor 和 virginica 的 p 值为小数点后 5 位,如何调整下面的代码以报告小数点后 3 位(即 0.009)?

library(tidyverse)
library(rstatix)
library(ggpubr)

test <- iris
test$Species <- as.factor(test$Species)
test.aov <- test %>% anova_test(Sepal.Width ~ Species)
test.tukey <- test %>% tukey_hsd(Sepal.Width ~ Species)
test.tukey <- test.tukey %>% add_xy_position(x = "Species")

ggboxplot(test, x = "Species", y = "Sepal.Width", outlier.shape = NA) +
  stat_pvalue_manual(test.tukey, hide.ns = TRUE, y.position = c(5,4.5,4), label = "p = {p.adj}") +
  geom_jitter(shape=16, alpha …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

0
推荐指数
1
解决办法
1374
查看次数

标签 统计

r ×5

ggplot2 ×3

dataframe ×1

tableau-api ×1

web-scraping ×1