我正在尝试在同一图表中为不同级别的组var定制多个黄土图的外观.我查看了这篇文章,但无法使其发挥作用:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(method = "loess")
Run Code Online (Sandbox Code Playgroud)
我想改变每个乐队和乐队的颜色.
如何让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="") + ...
我需要从 URL 下载一系列 Excel 文件,所有文件如下所示:
\nhttp://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\nRun Code Online (Sandbox Code Playgroud)\n\xc2\xa0
\n我在循环内有一些构建块,例如:
\nfor(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}\nRun Code Online (Sandbox Code Playgroud)\n\xc2\xa0
\n我的问题:
\nseq前面加上 0 (我尝试过sprintf但没有运气)\xc2\xa0
\n@akrun 解决方案效果很好。但事实证明,并非所有 Excel 文件都具有相同的列数:
\nmap(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) 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)