简体   繁体   中英

Plot multiple variables on scatter plot

I have created a data set that looks like:

> Data
   ZIP.Code Graduation.Rate  Residences  Tax_Payers   Businesses   Pot_Holes
1     60605      0.00876233 -0.43780716 -0.69756117 -0.705236816 -0.44078331
2     60607     -0.93779113 -0.62411552  0.45619155  0.429939383 -0.82886125
3     60608     -0.35352395  0.77774127  0.83458530  0.694500368  0.28789804
...

The values of each of these features has been scaled in [-1,1] range. I wish to have a scatter plot such that the x-axis has the labels of the ZIP_Code and the y-axis has 4 markers for each of the scaled values. How can I do this in R?

Melt your data to a long form so you can pass the data for all four variables to one aesthetic:

df_melt <- reshape2::melt(df, id.var = 'ZIP.Code')

# or
df_melt <- tidyr::gather(df, variable, value, Graduation.Rate:Pot_Holes)

df_melt
#    ZIP.Code        variable       value
# 1     60605 Graduation.Rate  0.00876233
# 2     60607 Graduation.Rate -0.93779113
# 3     60608 Graduation.Rate -0.35352395
# 4     60605      Residences -0.43780716
# 5     60607      Residences -0.62411552
# 6     60608      Residences  0.77774127
# 7     60605      Tax_Payers -0.69756117
# 8     60607      Tax_Payers  0.45619155
# 9     60608      Tax_Payers  0.83458530
# 10    60605      Businesses -0.70523682
# 11    60607      Businesses  0.42993938
# 12    60608      Businesses  0.69450037
# 13    60605       Pot_Holes -0.44078331
# 14    60607       Pot_Holes -0.82886125
# 15    60608       Pot_Holes  0.28789804

To plot with ggplot2

ggplot(df_melt, aes(x = factor(ZIP.Code), y = value, colour = variable)) + 
    geom_point() + xlab('ZIP Code')

绘图


To plot with base

with(df_melt, plot(ZIP.Code, value, col = variable, pch = 19))

底图

Note that it has some issues, including a pretty poorly arranged x-axis. Massaging the code can fix them, if you're really attached to base graphics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM