R 散点图 y 轴分组
R scatterplot y-axis grouped
我的 df 是一个包含个人(行)和他们花费的金额(列)的数据库 activity。我想在 R 中绘制具有以下特征的散点图:
x 轴:log(花费的金额)
y 轴:log(花费此金额的人数)
这是我得到的结果:
plot(log(df$Amount), log(df$???))
我该怎么做?谢谢!
我的 df 看起来像这样:
df
Name Surname Amount
John Smith 223
Mary Osborne 127
Mark Bloke 45
这就是我的想法(摘自陈(2012)的论文)
试试这个:
library(dplyr)
library(scales) # To let you make plotted points transparent
# Make some toy data that matches your df's structure
set.seed(1)
df <- data.frame(Name = rep(letters, 4), Surname = rep(LETTERS, 4), Amount = rnorm(4 * length(LETTERS), 200, 50))
# Use dplyr to get counts of loans in each 5-dollar bin, then merge those counts back
# into the original data frame to use as y values in plot to come.
dfsum <- df %>%
mutate(Bins=cut(Amount, breaks=seq(round(min(Amount), -1) - 5, round(max(Amount) + 5, -1), by=5))) # Per AkhilNair's comment
group_by(Bins) %>%
tally() %>%
merge(df, ., all=TRUE)
# Make the plot with the new df with the x-axis on a log scale
with(dfsum, plot(x = log(Amount), y = n, ylab="Number around this amount", pch=20, col = alpha("black", 0.5)))
生成的结果如下:
我的 df 是一个包含个人(行)和他们花费的金额(列)的数据库 activity。我想在 R 中绘制具有以下特征的散点图:
x 轴:log(花费的金额) y 轴:log(花费此金额的人数)
这是我得到的结果:
plot(log(df$Amount), log(df$???))
我该怎么做?谢谢!
我的 df 看起来像这样:
df
Name Surname Amount
John Smith 223
Mary Osborne 127
Mark Bloke 45
这就是我的想法(摘自陈(2012)的论文)
试试这个:
library(dplyr)
library(scales) # To let you make plotted points transparent
# Make some toy data that matches your df's structure
set.seed(1)
df <- data.frame(Name = rep(letters, 4), Surname = rep(LETTERS, 4), Amount = rnorm(4 * length(LETTERS), 200, 50))
# Use dplyr to get counts of loans in each 5-dollar bin, then merge those counts back
# into the original data frame to use as y values in plot to come.
dfsum <- df %>%
mutate(Bins=cut(Amount, breaks=seq(round(min(Amount), -1) - 5, round(max(Amount) + 5, -1), by=5))) # Per AkhilNair's comment
group_by(Bins) %>%
tally() %>%
merge(df, ., all=TRUE)
# Make the plot with the new df with the x-axis on a log scale
with(dfsum, plot(x = log(Amount), y = n, ylab="Number around this amount", pch=20, col = alpha("black", 0.5)))
生成的结果如下: