生成图例并连接数据点 (ggplot2)
我对 R 还很陌生。我(一整天)一直在尝试在一个图表上绘制数据点并为其生成图例。
我有一个关于人们的政治意识形态(因素,1=自由派,2=保守派,3=中性),企业选择(自由派或保守派)以及企业工资差异对数值(自由派企业工资 - 保守派企业工资)的原始数据集。
我试图做的是
- 根据提供工资的对数差异将人员任务划分为四分位数。
- 按人们的政治意识形态(3 种类型)划分每个四分位数。这给了我 4 X 3 组。
- 在一张图中绘制 12 个数据点。
我想
- 在一张图中绘制数据点,
- 生成图例,并
- 分别根据政治意识形态连接点。
因此,我计算了 12 个数据点(4 个为自由派,4 个为中立派,4 个为保守派)并使用 ggplot2 绘制它们。 我用蓝色代表自由派,用绿色代表中性,用红色代表保守派。
myggplot <- ggplot(, aes(x=c(-0.6, 0.55), y=c(0,1))) +
geom_point(aes(x=-0.4384035, y=0.3995726),col = "blue",shape = 15, size = 3) +
annotate("point", x=-0.221052, y=0.4463519, col="blue", shape=15, size=3)+
annotate("point", x=0.0839785, y=0.9610656, col="blue", shape=15, size=3)+
annotate("point", x=0.4146425, y=0.9598309, col="blue", shape=15, size=3)+
annotate("point", x=-0.4384035, y=0.1650485, col="green", shape=17, size=3)+
annotate("point", x=-0.221052, y=0.25, col="green", shape=17, size=3)+
annotate("point", x=0.0839785, y=0.8275862, col="green", shape=17, size=3)+
annotate("point", x=0.4146425, y=0.8152174, col="green", shape=17, size=3)+
annotate("point", x=-0.4384035, y=0.06818182, col="red", shape=16, size=3)+
annotate("point", x=-0.221052, y=0.08527132, col="red", shape=16, size=3)+
annotate("point", x=0.0839785, y=0.6377953, col="red", shape=16, size=3)+
annotate("point", x=0.4146425, y=0.7080292, col="red", shape=16, size=3)+
scale_color_manual(name="Political ideology",
values=c("Liberal"="blue", "Neutral"="green", "Conservative"="red"),
labels=c("Liberal", "Neutral", "Conservative"),
guide="legend")+
scale_shape_identity() +
labs(y="Probability of Choosing a Liberal Firm", x="Log Wage (Liberal Firm - Conservative Firm)",
title="The Effects of Log Wage Difference on Firm Choice") +
theme(
plot.title = element_text(size=10, hjust=0.5, face="bold"),
axis.title.x = element_text(size=10),
axis.title.y = element_text(size=10)
)
然而,即使我尝试了几个代码,我也无法生成图例。 这是我可以看到的:
我还尝试画三条线,每条线连接 4点(相同颜色)分别使用以下代码但失败。
myggplot + geom_line(mapping=aes(colour="blue", "green", "red", size=1))
它给了我一条错误消息:
Error: Discrete value supplied to continuous scale
I am pretty new to R. I've been trying (for the entire day) to plot data points on one graph and generate a legend for it.
I have a raw data set about people's political ideology (factor, 1=liberal, 2=conservative, 3=neutral), firm choice (liberal or conservative), and logarithm value of firm wage difference (liberal firm wage - conservative firm wage).
What I tried to do is
- Divide the people-tasks into quartiles according to the difference in log offered wages.
- Divide each quartile by people's political ideology (3 types). This gave me 4 X 3 groups.
- Plot 12 data points in one graph.
I wanted to
- plot data points in one graph,
- generate a legend, and
- connect dots based on the political ideology, respectively.
So, I computed 12 data points (4 for liberal, 4 for neutral, 4 for conservative) and plot them using ggplot2.
I used blue color for liberal, green color for neutral, and red color for conservative.
myggplot <- ggplot(, aes(x=c(-0.6, 0.55), y=c(0,1))) +
geom_point(aes(x=-0.4384035, y=0.3995726),col = "blue",shape = 15, size = 3) +
annotate("point", x=-0.221052, y=0.4463519, col="blue", shape=15, size=3)+
annotate("point", x=0.0839785, y=0.9610656, col="blue", shape=15, size=3)+
annotate("point", x=0.4146425, y=0.9598309, col="blue", shape=15, size=3)+
annotate("point", x=-0.4384035, y=0.1650485, col="green", shape=17, size=3)+
annotate("point", x=-0.221052, y=0.25, col="green", shape=17, size=3)+
annotate("point", x=0.0839785, y=0.8275862, col="green", shape=17, size=3)+
annotate("point", x=0.4146425, y=0.8152174, col="green", shape=17, size=3)+
annotate("point", x=-0.4384035, y=0.06818182, col="red", shape=16, size=3)+
annotate("point", x=-0.221052, y=0.08527132, col="red", shape=16, size=3)+
annotate("point", x=0.0839785, y=0.6377953, col="red", shape=16, size=3)+
annotate("point", x=0.4146425, y=0.7080292, col="red", shape=16, size=3)+
scale_color_manual(name="Political ideology",
values=c("Liberal"="blue", "Neutral"="green", "Conservative"="red"),
labels=c("Liberal", "Neutral", "Conservative"),
guide="legend")+
scale_shape_identity() +
labs(y="Probability of Choosing a Liberal Firm", x="Log Wage (Liberal Firm - Conservative Firm)",
title="The Effects of Log Wage Difference on Firm Choice") +
theme(
plot.title = element_text(size=10, hjust=0.5, face="bold"),
axis.title.x = element_text(size=10),
axis.title.y = element_text(size=10)
)
However, I cannot generate a legend even after I tried several codes.
This is what I can see:
I also tried to draw three lines, each connecting 4 dots (of the same color) respectively using the following code but failed.
myggplot + geom_line(mapping=aes(colour="blue", "green", "red", size=1))
It gives me an error message:
Error: Discrete value supplied to continuous scale
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行以下操作:
请注意,这要求您的数据位于框架
df
中,该框架应如下所示:输入:
You can do something like this:
Note, that this requires your data to be in the frame
df
, which should look like this:Input:
可以通过稍微修改代码来完成,但正如已经提到的,这不是最好的方法。
It can be done by modifying your code slightly, but as has already been mentioned it's not the best way.