以不同颜色绘制多列CSV数据 [英] Plotting multiple column csv data in different colors
问题描述
Year CRMale CRFemale CRTotal MMale MFemale MTotal
1972 531 529 530 527 489 509
1973 523 521 523 525 489 506
1974 524 520 521 524 488 505
...
这是我的csv数据格式我将年份设置为水平轴,并将分数设置为垂直轴
This is my csv data format I am setting the year as my horizontal axis and scores as vertical axis
sat <- read.csv("Table 2_9FIXED.csv")
graph_base3 <- ggplot(data = sat, aes(x = Year, y = CRMale))
我可以创建一个包含1列数据的散点图,但是我一直坚持以不同的颜色同时绘制多列.
I could create a scatter plot graph of 1 column data, but I am stuck on plotting multiple columns in same time in different colors.
有人可以帮我吗?我对R完全陌生.
Can anyone help me on this? I am totally new to R.
推荐答案
您需要转换数据(融化"数据).
You need to transform your data ("melt" it).
# Transform data using melt from reshape2 package
library(reshape2)
# We melt by column "Year"
satMelt <- melt(sat, "Year")
# Plot
ggplot(satMelt, aes(Year, value, color = variable)) +
geom_point()
如果您不想使用颜色,则可以使用构面:
If you don't want to use color then you can use facets:
ggplot(satMelt, aes(Year, value)) +
geom_point() +
facet_wrap(~ variable, ncol = 2)
PS:这是融化"数据的样子:
PS: This is what "melted" data looks like:
# Year variable value
# 1972 CRMale 531
# 1973 CRMale 523
# 1974 CRMale 524
# 1972 CRFemale 529
# 1973 CRFemale 521
# ...
我注意到您的数据中有几组(例如性别").
I noticed that there are groups in your data (eg. "gender").
我们可以提取以下信息:
We can extract this information:
satMelt$gender <- sub("^CR|^M", "", satMelt$variable)
satMelt$type <- sub(paste(unique(satMelt$gender), collapse = "|"), "", satMelt$variable)
# Year variable value gender type
# 1972 CRMale 531 Male CR
# 1973 CRMale 523 Male CR
# 1974 CRMale 524 Male CR
# 1972 CRFemale 529 Female CR
# 1973 CRFemale 521 Female CR
# 1974 CRFemale 520 Female CR
并使用它来创建类似这样的情节:
And use it to create plot like this:
ggplot(satMelt, aes(Year, value, color = gender, linetype = type, shape = type)) +
geom_point() +
geom_line()
要使绘图更具视觉吸引力,我们可以尝试以下方法:
And to make plot more visual appealing we can try this:
ggplot(satMelt, aes(Year, value, color = gender, linetype = type)) +
geom_point(size = 3, alpha = 0.6) +
geom_line(size = 1, alpha = 0.8) +
scale_x_continuous(breaks = sat$Year) +
labs(title = "Change in value over years",
subtitle = "By gender and type",
y = "Value",
color = "Gender",
linetype = "Type") +
theme_minimal()
这篇关于以不同颜色绘制多列CSV数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!