【问题标题】:how can i make the circles of my plot smaller in R?如何在 R 中使我的情节的圆圈更小?
【发布时间】:2020-10-12 21:06:22
【问题描述】:

这是我使用的代码:

resources <- read.csv("https://raw.githubusercontent.com/umbertomig/intro-prob-stat-FGV/master/datasets/resources.csv")

res <- subset(resources, select = c("cty_name", "year", "regime",
                             "oil", "logGDPcp", "illit"))
resNoNA <- na.omit(res)
resNoNAS <- scale(resNoNA[, 3:6])
colMeans(resNoNA[, 3:6])
apply(resNoNA[, 3:6], 2, sd)
cluster2 <- kmeans(resNoNAS, centers = 2)
table(cluster2$cluster)
## this gives standardized answer, which is hard to interpret
cluster2$centers
## better to subset the original data and then compute means
g1 <- resNoNA[cluster2$cluster == 1, ]
colMeans(g1[, 3:6])
g2 <- resNoNA[cluster2$cluster == 2, ]
colMeans(g2[, 3:6])

plot(x = resNoNA$logGDPcp, y = resNoNA$illit, main = "Illiteracy v GDP",
xlab = "GDP per Capita",  ylab = "Illiteracy", 
col = cluster2$cluster, cex = resNoNA$oil)

但我想让圆圈变小以适应图形的限制

【问题讨论】:

  • 关于 SO(尤其是在 R 中)的问题如果是可重复的和独立的,则效果会更好。我的意思是包括尝试的代码(请明确说明非基础包),示例代表性数据(可能通过dput(head(x)) 或以编程方式构建数据(例如,data.frame(...)),可能在set.seed(1) 之后随机生成),可能是实际输出(带有逐字错误/警告)与预期输出。参考:stackoverflow.com/q/5963269、minimal reproducible example 和 stackoverflow.com/tags/r/info。 (你提供的……谁知道呢。)
  • 也许这个对你有帮助:stackoverflow.com/questions/2579995/… ?

标签: r plot cluster-analysis k-means scatter-plot


【解决方案1】:

您可以在此处使用cex= 控制圆直径。

plot(x = resNoNA$logGDPcp, y = resNoNA$illit, main = "Illiteracy v GDP",
xlab = "GDP per Capita",  ylab = "Illiteracy", 
col = cluster2$cluster, cex = resNoNA$oil)
plot(x = resNoNA$logGDPcp, y = resNoNA$illit, main = "Illiteracy v GDP",
xlab = "GDP per Capita",  ylab = "Illiteracy", 
col = cluster2$cluster, cex = resNoNA$oil/3)
plot(x = resNoNA$logGDPcp, y = resNoNA$illit, main = "Illiteracy v GDP",
xlab = "GDP per Capita",  ylab = "Illiteracy", 
col = cluster2$cluster, cex = resNoNA$oil/5)

但是请注意,如果您在某些自动报告生成器中使用它(例如,rmarkdown、shiny),那么您可能需要调整绘图的尺寸以从另一个角度控制它:更新xlim 和 ylim。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2021-09-01
    • 2021-10-20
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多