小编Luk*_*kas的帖子

R,ggplot:y轴上的小数

我想制作一个条形图,类似于这个MWE:

library(tidyverse)
library(ggplot2)

mtcars %>% 
  mutate(mpg=mpg/1000) %>% 
  ggplot(aes(x=cyl, y=mpg)) +
  geom_bar(stat="identity") +
  scale_y_continuous(labels = scales::percent)
Run Code Online (Sandbox Code Playgroud)

我得到的是以下内容(请记住,这是无稽之谈,但用于说明目的): 在此输入图像描述 现在,我希望从y轴上的百分比替换小数("30%"而不是"30.0%").我能做什么?

我在这里发现了一个类似的问题,但无法使该功能NRPercent不起作用(并且不能在那里发表评论).

r ggplot2

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

数据框中列总和的条形图

I have a data frame with 190 observations (rows). There are five columns, in which every entry either has the value 0 or 1.
How do I get a barplot that has the name of the five columns on the x-Axis and the number of 1's (i.e. the sum) of every column as height of the bars - preferably with ggplot?
I'm sorry I don't have any sample data, I couldn't figure out how to produce a smaller dataframe that …

r bar-chart ggplot2 dataframe cptbarplot

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

计算平均值和其他排除某些值

我有一个日期框架(“daten”),其中大多数列都是数值。它们的范围通常从 0 到 5。但是,它们也可以取值 99。我想计算列的平均值,仅排除值 99。

例如:

> mean(c(0, 1, 2, 3, 4, 5, 99))
[1] 16.28571
Run Code Online (Sandbox Code Playgroud)

不是我需要的,而是我希望它被计算为好像向量是

> mean(c(0, 1, 2, 3, 4, 5))
[1] 2.5
Run Code Online (Sandbox Code Playgroud)

,给我我正在寻找的意思。

有一个类似的问题(通过排除任何给定数字来计算平均值,中位数),但该解决方案对我不起作用。然而,我想,一旦我可以排除任何列中的某个值,我就可以简单地将它与 组合apply,所以我实际上正在寻找一种方法来计算某个向量的平均值,但忽略了某些值。

r mean dataframe

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

使用列方法将函数应用于矩阵中的所有列

我有一个矩阵m(由三列组成,每列由整数组成,平均值分别为5,10和15):

m <- round(matrix(data=cbind(rnorm(30, 5), rnorm(30, 10), rnorm(30, 15)), nrow=30, ncol=3), 0)

现在,我希望有一个与m相同尺寸的矩阵,其中每个值x计算为x减去找到x的列的平均值.如何才能做到这一点?我尝试过应用各种应用函数(具体来说,我一直在查看user3640617User60的两个问题),但问题似乎是我不能使用行的平均值作为参数sapply,lapply或者vapply......

示例:如果head(m)

     [,1] [,2] [,3]
[1,]    6   11   14
[2,]    6    8   16
[3,]    6   11   15
[4,]    6   10   17
[5,]    5    9   15
[6,]    3   10   15
Run Code Online (Sandbox Code Playgroud)

我想得到

     [,1] [,2] [,3]
[1,]    1    1   -1
[2,]    1   -2    1
[3,]    1    1    0
[4,]    1    0    2
[5,]    0   -1    0
[6,]   -2    0 …
Run Code Online (Sandbox Code Playgroud)

r apply lapply sapply

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

标签 统计

r ×4

dataframe ×2

ggplot2 ×2

apply ×1

bar-chart ×1

cptbarplot ×1

lapply ×1

mean ×1

sapply ×1