【问题标题】:Draw a graph to emphasize the difference current and expected values in R绘制图表以强调 R 中的差异电流和期望值
【发布时间】:2020-03-06 11:00:20
【问题描述】:

我有一个预期收入和当前收入的数据集:

 id currentsalary expectedsalary
1   1            NA           1500
2   2            NA           3000
3   3            NA             NA
4   4            NA             NA
5   5            NA           1500
6   6          1500           3000
7   7            NA           1500
8   8            NA           5000
9   9          1000           1500
10 10          3000           5000

我想显示预期净收入相对于当前净收入的分布(图表+结论)。我画直方图:

hist(df$expectedsalary, col="pink", xlab="salary")
hist(df$currentsalary, col="blue", add=T)

但它没有正确显示关系。我想将 id 放在 x 坐标以及 y 轴上的当前和预期薪水(可能是直方图上的一条线),以强调基于个人的预期薪水和当前薪水之间的差异。我该怎么做?

【问题讨论】:

标签: r ggplot2 line histogram


【解决方案1】:

我会使用点图来绘制差异:

说明性数据

set.seed(122)
df <- data.frame(
  id = 1:10,
  exp = sample(1000:5000, 10),
  curr = sample(800: 4500, 10)
)

解决方案

计算差值:

df$diff <- df$curr - df$exp

画点图:

dotchart(df$diff, labels = df$id, main = "Difference in current v expected income",
         col = ifelse(df$diff < 0, "red", "blue"), density = 50, angle = 90)
abline(v = 0)

结果

(显然这个可以大大修饰)

编辑

使用条形图怎么样?

barplot(df$diff, names = df$id, xlab = "ID", ylab = "Difference", 
        main = "Difference in current v expected income",
        col = ifelse(df$diff < 0, "red", "blue"), density = 50, angle = 90)
legend("topright", c("Current > Expected income", "Current < Expected income"), 
       fill = c("blue", "red"),
       cex = 0.8)

结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 2018-01-11
    • 2020-06-21
    相关资源
    最近更新 更多