如何使用3个变量绘制“数据透视表”,使用观察数量作为点的大小?

时间:2017-10-29 16:22:38

标签: r plot ggplot2 pivot-table

我一直试图通过几条路线,但没有运气。

我有两列,“Type”(字符串)和“Units”(整数)。我希望x轴是Type,Y轴是每种类型的单位的,以及基于每种类型的观察数量的点的大小(gapminder样式)

这是我到目前为止所尝试的内容:

  1. plot(xtabs(Type~Monits)):这得到了正确的x / y关系,但是我没有看到如何包含要用观察数表示的形状/大小
  2. ggplot(数据,aes(类型,单位))+ geom_count():后退半步,因为我已经丢失了总和,但至少现在我知道哪种类型有更多的观察结果。
  3. 在某种程度上,我想要一个静态的gapminder图。

    理想情况下,我想用ggplot2(或者除了默认值之外的任何东西)来做,因为图表更好;但我会接受任何指示。

    我感觉非常接近,但我不确定我错过了什么。

    更新:

    structure(list(CONTRIBUTOR_NAME = c("DATA LTD", 
    "GEORGE", "PHIL", "JOHN J LOVE", 
    "E LOVE", "MADISON LTD", "NAOMI", 
    " HARRISON", " GILL", "RON R ", "MARIBETH ", 
    "BEV", "P ANN", "DYCK", "GEHRING", 
    "HARVIE ", "SUSAN", "JANE", "RANDY C", 
    "GEORGE N "), DATE = structure(c(13511, 15826, 14461, 
    16491, 12874, 15295, 15881, 13466, 14373, 16560, 15223, 13518, 
    14096, 16555, 15861, 15644, 15923, 13153, 15741, 15091), class = "Date"), 
        AMOUNT = c(500, 200, 12.5, 30, 300, 332, 10, 10, 100, 23, 
        20, 22, 25, 10, 25, 50, 10, 40, 35, 150), CLASS = c("Corporations", 
        "Individuals", "Individuals", "Individuals", "Individuals", 
        "Corporations", "Individuals", "Individuals", "Individuals", 
        "Individuals", "Individuals", "Individuals", "Individuals", 
        "Individuals", "Individuals", "Individuals", "Individuals", 
        "Individuals", "Individuals", "Individuals"), PARTY = c("PARTY 5", 
        "PARTY 1", "PARTY 1", "PARTY 1", "PARTY 1", "PARTY 5", 
        "PARTY 1", "PARTY 5", "PARTY 5", "PARTY 1", 
        "PARTY 1", "PARTY 1", "PARTY 1", "PARTY 5", "PARTY 1", 
        "PARTY 5", "PARTY 1", "PARTY 1", "PARTY 1", "PARTY 1"
        ), BUSINESS = c("Businesses", "Individuals", "Individuals", 
        "Individuals", "Individuals", "Businesses", "Individuals", 
        "Individuals", "Individuals", "Individuals", "Individuals", 
        "Individuals", "Individuals", "Individuals", "Individuals", 
        "Individuals", "Individuals", "Individuals", "Individuals", 
        "Individuals")), .Names = c("CONTRIBUTOR_NAME", "DATE", "AMOUNT", 
    "CLASS", "PARTY", "BUSINESS"), row.names = c(NA, -20L), class = c("tbl_df", 
    "tbl", "data.frame"))
    

1 个答案:

答案 0 :(得分:1)

您没有提供您的数据,因此我无法用此说明。我将使用内置的mtcars数据。您可以使用cex获取每种类型的观察次数,然后使用plot(unique(mtcars$cyl), as.vector(xtabs(mpg ~ cyl, data=mtcars)), xlim=c(3.5,8.5), ylim=c(130,310), pch=20, xlab="Cylinders", ylab="Sum of MPG", cex = table(mtcars$cyl), col="#0000FF44") 参数调整磅值。

df=pd.DataFrame(rand(10))
x= arange(10)
valid,forecast = x<=4,x>=4
plot(x,ma.MaskedArray(df,~valid),'b',x,ma.MaskedArray(df,~forecast),'r--')

Gapminder style