2015-07-28 151 views
0

我的df是一個活動中個人(行)和他們花費的數量(列)的數據庫。我想提請R中的散點圖,具有以下特點:R scatterplot y軸分組

X軸:LOG(消費金額) y軸:登錄

(那度過這一數額人數)這是多遠我得到了:

plot(log(df$Amount), log(df$???)) 

我該怎麼做?謝謝!

我DF看起來是這樣的:

df 
    Name Surname Amount 
    John  Smith  223 
    Mary Osborne 127 
    Mark  Bloke  45 

這就是我心目中

enter image description here

+0

提供一個可再現的代碼示例可能很有用。那麼,如何死掉'df'的樣子。 – drmariod

+0

@ drmariod編輯 – Billaus

+0

hm,所以你希望有更多的人花費223的金額?在這種情況下,'table(df $ Amount)'會返回數字。 – drmariod

回答

1

(陳(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))) 

以下是生產的產品: enter image description here