小编Tje*_*ebo的帖子

得到R曲线类型"b"

在ggplot2中有一种方法可以获得情节类型"b"吗?见例子:

x <- c(1:5)
y <- x 
plot(x,y,type="b")
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想用它们的值替换点,以获得类似于这个着名示例的东西:

替代文字

编辑:这里有一些样本数据(我想在情节类型为"b"的方面绘制每个"猫"):

df <- data.frame(x=rep(1:5,9),y=c(0.02,0.04,0.07,0.09,0.11,0.13,0.16,0.18,0.2,0.22,0.24,0.27,0.29,0.31,0.33,0.36,0.38,0.4,0.42,0.44,0.47,0.49,0.51,0.53,0.56,0.58,0.6,0.62,0.64,0.67,0.69,0.71,0.73,0.76,0.78,0.8,0.82,0.84,0.87,0.89,0.91,0.93,0.96,0.98,1),cat=rep(paste("a",1:9,sep=""),each=5))
Run Code Online (Sandbox Code Playgroud)

此致,穆萨

r ggplot2

5
推荐指数
2
解决办法
1537
查看次数

仅从 data.frame 中的某些选定列返回包含最大值的列的名称

我想获取(在 data.table 的新列中)包含 data.frame 中仅几列中的最大值的列的列名。

这是一个示例 data.frame

# creating the vectors then the data frame ------
id = c("a", "b", "c", "d")
 ignore = c(1000,1000, 1000, 1000) 
 s1 = c(0,0,0,100)
s2 = c(100,0,0,0)
s3 = c(0,0,50,0)
s4 = c(50,0,50,0)
 df1 <- data.frame(id,ignore,s1,s2,s3,s4)  
Run Code Online (Sandbox Code Playgroud)

(1) 现在我想从 s1-s4 列中找到每行中最大数字的列名。(即忽略名为“忽略”的列)

(2) 如果最大值并列,我希望返回最后一个(例如 s4)列名。

(3) 作为一个额外的好处 - 如果都是 0,我希望 NA 返回

这是我迄今为止最好的尝试

df2 <- cbind(df1,do.call(rbind,apply(df1,1,function(x) {data.frame(max.col.name=names(df1)[which.max(x)],stringsAsFactors=FALSE)})))
Run Code Online (Sandbox Code Playgroud)

这在每种情况下都会返回忽略,并且(b 行除外)如果我删除此列,并将 s1-s4 列重新排序为 s4-s1,则有效。

你会如何处理这个问题?

确实非常感谢。

r dataframe

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

R lapply():将列表中所有数据框中的所有列更改为数字,然后将所有值转换为百分比

题:

我对如何为数据框列表中的列批量处理 as.numeric() (或任何其他函数)感到有些困惑。

我知道我可以使用以下方法查看此列表中的特定数据框或列:

> my.list[[1]] 
# or columns within this data frame using:
> my.list[[1]][1]
Run Code Online (Sandbox Code Playgroud)

但是当我尝试将其应用于 lapply() 函数以将所有数据从整数更改为数字时,我的麻烦就来了。

# Example of what I am trying to do
> my.list[[each data frame in list]][each column in data frame] <- 
as.numberic(my.list[[each data frame in list]][each column in data frame])
Run Code Online (Sandbox Code Playgroud)

如果您能以任何方式帮助我,或者知道任何可以帮助我的资源,我将不胜感激。

背景:

我的数据框的结构如下例所示,其中我有 5 种栖息地类型以及有关单个物种家庭范围扩展到n 的面积的信息:

# Example data
spp.1.data <- data.frame(Habitat.A = c(100,45,0,9,0), Habitat.B =  c(0,0,203,45,89), Habitat.C = c(80,22,8,9,20), Habitat.D = c(8,59,77,83,69), Habitat.E = c(23,15,99,0,10))
Run Code Online (Sandbox Code Playgroud)

我有多个具有上述结构的数据框,它们已分配给列表对象:

all.spp.data <- …
Run Code Online (Sandbox Code Playgroud)

r lapply

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

基于值的 Geom_jitter 颜色

有没有办法根据数值为箱线图上的抖动点着色,例如:

ggplot(data, aes(y = y_var, x = x_var)) +
  geom_jitter(size = 2, aes(color = ifelse(y_var < 5, "red", "black)))
Run Code Online (Sandbox Code Playgroud)

我添加了这个可重现的示例,但不太有效(绘图上的颜色与抖动调用不对应):

a <- rnorm(100, mean = 5, sd = 1)
b <- as.factor(sample(0:1, 100, replace = TRUE))
test_data <- data.frame(cbind(a,b))
test_data$b <- as.factor(test_data$b)

ggplot(test_data, aes(y = a, x = b)) + 
  geom_boxplot()+
  geom_jitter(aes(color = ifelse(a < 5, "red", "black")))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r ggplot2 jitter

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

将字母显示为 geom_text 图例的关键字形而不是默认的“a”

我正在使用 geom_raster 和 geom_text 在我的情节中的每个彩色矩形上放置一个字母。我也希望这封信出现在图例中的颜色框顶部,但不知道如何。

我尝试添加show.legend=TRUEgeom_text,但这会导致每个图例键中的字母“a”,而不是所需的字符。

所需的结果如下所示:

想要的结果

这是重现基本情节的代码:

library(tidyverse)
d <-tribble(
    ~a, ~b, ~c,
    "a", "l", "A",
    "a", "r", "F",
    "b", "l", "Q",
    "b", "r", "R"
)

ggplot(data=d, aes(x=a, y=b, fill=c)) +
    geom_raster(na.rm=TRUE) +
    geom_text(aes(label=c), size=3, na.rm=TRUE) 
Run Code Online (Sandbox Code Playgroud)

和输出:

没有所需标签的示例

这可能与此问题有关:https://github.com/tidyverse/ggplot2/issues/2004,但也许有解决方法?

r legend ggplot2

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

如何在ggplot2 R中向轴添加子文本

对于主y轴和x轴,我有通用标题,如"Tank's Ratio"和"Counts".我想要第二行标签,我指定比率和计数.例如.就在"Tank's Ratio"之下,我希望"#in water /#in sand"中的字体较小,但是沿着y轴.类似地,对于x轴.这是基本代码

data <- data.frame(set = c(1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4), density = c(1, 3, 3, 1, 3, 1, 1, 1, 3, 3, 1, 3), counts = c(100, 2, 3, 76, 33, 12, 44, 13, 54, 36, 65, 1), ratio = c(1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 90, 1))

library(ggplot2)

ggplot(data, aes(x = counts, y = ratio)) + 
  geom_point() + 
  ylab("Tank's Ratio") + 
  xlab("Counts") 
Run Code Online (Sandbox Code Playgroud)

r axes subtitle ggplot2

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

ggplot:在轴标题上方放置刻面条

我想创建一个图,其中轴标题位于顶部/左侧,轴标签位于底部/右侧。这可以通过创建一个重复的轴并删除不需要它们的边上的轴标题和标签(通过主题选项)来完成。

然而,在将 x 轴标题放在多面图中的顶部时,我遇到了一个问题。出于某种原因,尽管使用了选项,但刻面的条形文本仍位于轴标题下方strip.placement="outside"。有趣的是,如果我不隐藏轴标签(见下图),它们在条形文本下方,但轴标题在条形文本上方。

知道如何将轴标题也移动到 strip.text 下方吗?请注意,我正在使用 ggplot ggplot2_3.3.0.9000。非常感谢。

library(tidyverse)

iris %>% 
  ggplot()+
  geom_bar(aes(x=Sepal.Length,
               y=Sepal.Width),
           stat="identity")+
  facet_wrap(~Species,
             strip.position = "top")+
  scale_y_continuous(sec.axis = dup_axis())+  #create secondary axis
  scale_x_continuous(sec.axis = dup_axis())+  #create secondary 
  theme(strip.placement = "outside",
        axis.title.x.top = element_text(hjust=0),  #left align axis title on top
        axis.title.x.bottom = element_blank(), #remove title on x axis/bottom
        axis.ticks.length.x.top = unit(0, units="cm"), #remove axis ticks on top
        axis.text.y.left = element_blank(), #remove axis label on left side
        axis.title.y.right = element_blank(), #remove axis title on right side …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

是否有类似 Expand.grid 的矩阵输出函数

只是好奇是否有任何东西 - 一个expand.grid类似的函数会返回一个矩阵而不是一个数据框?

预期的输出(但不是达到预期的方式

as.matrix(expand.grid(1:4,1:4))
#>       Var1 Var2
#>  [1,]    1    1
#>  [2,]    2    1
#>  [3,]    3    1
#>  [4,]    4    1
#>  [5,]    1    2
#>  [6,]    2    2
#>  [7,]    3    2
#>  [8,]    4    2
#>  [9,]    1    3
#> [10,]    2    3
#> [11,]    3    3
#> [12,]    4    3
#> [13,]    1    4
#> [14,]    2    4
#> [15,]    3    4
#> [16,]    4    4
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-03-25 …

r

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

将 filter_all(any_vars()) 转换为 filter(across())

在更新我自己对另一个线程的答案时,我无法想出一个好的解决方案来替换最后一个示例(见下文)。这个想法是获取任何列包含某个字符串的所有行,在我的示例“V”中。

library(tidyverse)

#get all rows where any column contains 'V'
diamonds %>%
  filter_all(any_vars(grepl('V',.))) %>%
  head
#> # A tibble: 6 x 10
#>   carat cut       color clarity depth table price     x     y     z
#>   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
#> 2 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
#> 3 0.24  Very Good J     VVS2     62.8 …
Run Code Online (Sandbox Code Playgroud)

r dplyr

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

如何使用 sf 更改国家之间共享边界的颜色?

我想将共享颜色更改为不同的颜色,比如说红色。到目前为止,我正在绘制德国联邦州巴伐利亚并触及奥地利各州。我从https://gadm.org/download_country.html获取数据-

\n

德国 2 级 - https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_DEU_2_sf.rds

\n

德国 1 级 - https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_DEU_1_sf.rds

\n

奥地利 2 级 - https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_AUT_2_sf.rds

\n

奥地利 1 级 -\n https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_AUT_1_sf.rds

\n
library("sf")\nlibrary("raster")\nlibrary("dplyr")\nlibrary("spData")\nlibrary("spDataLarge")\nlibrary("ggplot2")\nlibrary("patchwork")\nlibrary(tmap)    # for static and interactive maps\nlibrary(ggpattern)\n\ndata_aut <- readRDS("~/plot_at_ger/data/gadm36_AUT_2_sf.rds")\ndata_ger <- readRDS("~/plot_at_ger/data/gadm36_DEU_2_sf.rds")\ndata_aut_high <- readRDS("~/plot_at_ger/data/gadm36_AUT_1_sf.rds")\ndata_aut_high <- data_aut_high[which(data_aut_high$NAME_1==\'Salzburg\' | data_aut_high$NAME_1==\'Ober\xc3\xb6sterreich\' | data_aut_high$NAME_1==\'Tirol\' | data_aut_high$NAME_1==\'Vorarlberg\'), ]\ndata_ger_high <- readRDS("~/plot_at_ger/data/gadm36_DEU_1_sf.rds")\ndata_ger_high <- data_ger_high[which(data_ger_high$NAME_1==\'Bayern\'), ]\n\nggplot() +\n  geom_sf(data = ger_selected_data_bavaria, fill = NA) +\n  geom_sf(data = aut_selected_data_rel, fill = NA) +\n  geom_sf(data = data_aut_high, fill = NA, size = 1, color …
Run Code Online (Sandbox Code Playgroud)

maps r ggplot2 r-sf

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

标签 统计

r ×10

ggplot2 ×6

axes ×1

dataframe ×1

dplyr ×1

jitter ×1

lapply ×1

legend ×1

maps ×1

r-sf ×1

subtitle ×1