小编Tho*_*del的帖子

ggplot stat_smooth:改变多个乐队的外观

我正在尝试在同一图表中为不同级别的组var定制多个黄土图的外观.我查看了这篇文章,但无法使其发挥作用:

ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) + 
  stat_smooth(method = "loess")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想改变每个乐队和乐队的颜色.

graphics r ggplot2

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

ggplot显示因子级别的相同颜色

如何让ggplot始终对因子使用相同的颜色映射.例如:

library(ggplot)

## Filter for visual simplification
diamonds2 <- diamonds[1:10,]
ggplot(diamonds2, aes(carat, price, color = cut)) + geom_point()

## Now filtering removes some levels of cut
diamonds3 <- diamonds[1:5,]
ggplot(diamonds3, aes(carat, price, color = cut)) + geom_point()
Run Code Online (Sandbox Code Playgroud)

在第一个散点图因子级别中,"Fair"为红色.在第二张图中,因子水平"好"变为红色.我希望保留映射,无论过滤是否删除因子级别,以便"Fair"始终映射为红色等等.

实际上我的ggplot要复杂得多.我的原始因素有11个级别.

MyPalette <- c("#5DD0B9", "#E1E7E9", "#1f78b4", "#a6cee3",  "#de77ae", "#c51b7d", "#8e0152", "#6a3d9a", "#fbdf6f", "#ff7f00", "#fff99")
Run Code Online (Sandbox Code Playgroud)

我在ggplot中提到的

... scale_fill_manual(values = MyPalette, name="") + ...

plot1 plot2

r ggplot2

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

使用 R 循环遍历 url

我需要从 URL 下载一系列 Excel 文件,所有文件如下所示:

\n
http://example.com/orResultsED.cfm?MODE=exED&ED=01&EventId=31\nhttp://example.com/orResultsED.cfm?MODE=exED&ED=02&EventId=31\n...\nhttp://example.com/orResultsED.cfm?MODE=exED&ED=87&EventId=31\n
Run Code Online (Sandbox Code Playgroud)\n

\xc2\xa0

\n

我在循环内有一些构建块,例如:

\n
for(i in 1:87) {\n    url <- paste0("http://example.com/orResultsED.cfm?MODE=exED&ED=", i, "&EventId=31")\n    file <- paste0("Data/myExcel_", i, ".xlsx")\n    if (!file.exists(file)) download.file(url, file) \n}\n
Run Code Online (Sandbox Code Playgroud)\n

\xc2\xa0

\n

我的问题

\n
    \n
  1. 我需要在seq前面加上 0 (我尝试过sprintf但没有运气)
  2. \n
  3. 我还想导入 Excel 文件,跳过前两行并将它们附加到另一行之后(它们也具有相同的列)
  4. \n
\n

\xc2\xa0

\n

更新

\n

@akrun 解决方案效果很好。但事实证明,并非所有 Excel 文件都具有相同的列数:

\n
map(files, ~read.xlsx(.x, \n                         colNames = FALSE,\n                         sheet = 1, \n                         startRow = 4,\n                         )) %>%\n  bind_rows\n\nError in bind_rows_(x, .id) : …
Run Code Online (Sandbox Code Playgroud)

loops r web-scraping rvest

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

Single row per id to multiple row per id

I'd like to expand observations from single row-per-id to multiple rows-per-id based on a given time interval:

> dput(df)
structure(list(id = c(123, 456, 789), gender = c(0, 1, 1), yr.start = c(2005, 
2010, 2000), yr.last = c(2007, 2012, 2000)), .Names = c("id", 
"gender", "yr.start", "yr.last"), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -3L))
> df
# A tibble: 3 x 4
     id gender yr.start yr.last
  <dbl>  <dbl>    <dbl>   <dbl>
1   123      0     2005    2007
2   456      1     2010    2012 …
Run Code Online (Sandbox Code Playgroud)

r reshape dataframe melt dplyr

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

标签 统计

r ×4

ggplot2 ×2

dataframe ×1

dplyr ×1

graphics ×1

loops ×1

melt ×1

reshape ×1

rvest ×1

web-scraping ×1