如何在ggplot2中创建图例并更改点形状?
重现下图
我需要在 R Studio 上为宏观经济学项目 。我已经能够弄清楚其中的大部分内容,但给我带来最大问题的是图例和改变数据点的形状。这是到目前为止我所拥有的
手动输入的数据点
# `First I load up some packages`
library(tidyverse)
# `then I create vectors for the years and percentages for Europe and the US`
year <- c(1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 200, 2010)
us_percent <- c(40.5, 40.9, 44.7, 45.1, 36.5, 33.7, 34.1, 33.4, 37.5, 42.4, 46.9, 48.8)
euro_percent <- c(45.9, 45.8, 39.5, 40.5, 33.8, 31.7, 31.6, 29.7, 29.4, 32.4, 34, 34.7)
# `then I create a data frame for my vectors and name it`
df <- data.frame(year, us_percent, euro_percent)
# `then I create a ggplot function for the US and Europe inequality`
ggplot() +
geom_line(data=df, mapping=aes(x=year, y=euro_percent), color="cyan") +
geom_point(data=df, mapping=aes(x=year, y=euro_percent), color="cyan", ) +
geom_line(data=df, mapping=aes(x=year, y=us_percent), color="purple") +
geom_point(data=df, mapping=aes(x=year, y=us_percent), color="purple") +
xlim(1900, 2010) +
ylim(25, 50) +
labs(x="Year", y="Share of Total Pretax Income", title="Income Inequality")
这是我的代码,其中包含我尝试运行的 各种版本的 scale_color_manual
程序,但它会留下它,所以我的 R 控制台会显示一个“+”号,我不知道之后要添加什么。任何建议都将受到欢迎。谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将您的数据转换为长格式,并将货币映射到形状和颜色美学。
选择颜色、尺寸和主题也是一个好主意,使您的数据和系列之间的差异清晰可见
< img src="https://i.sstatic.net/vH0zX.png" alt="在此处输入图像描述">
Pivot your data to long format and map the currency to shape and color aesthetics.
It's also a good idea to pick colors, sizes and themes that make your data and the differences between series clearly visible
您可以使用
shape =
参数。通常,使用ggplot2
,建议在绘制之前以“长”格式使用数据集 - 如果您在aes()作为形状和颜色。
You can use the
shape =
argument. In general withggplot2
it's advisable to have a dataset in "long" format before plotting - much easier to then add a legend, if you add the stratification variables in theaes()
as shapes and colours.不要手动指定颜色(在您的情况下为“青色”和“紫色”),而是将数据从“宽”转换为“长”,并创建一个新变量,将每个观察结果分类为“美国”或“欧元”。这可以使用 dplyr 的
gather()
来完成(也修复了你的拼写错误):然后,当你的数据设置并准备好时,你可以简单地定义功能美学(形状、颜色)在主 ggplot() 函数中,如下所示:
现在你的代码更少了,并且有更多的空间来进行一些额外的美学改变,如果你想。
Instead of manually specifying the color (in your case, "cyan" and "purple") transform your data from 'wide' to 'long' and create a new variable that categorizes each observation into "US" or "EURO". This can be done with the
gather()
from dplyr (fixed your typos as well):Then, when you're data is set and ready, you can simply define the functional aesthetics (shape, color) within the main
ggplot()
function, like so:Now you've got less code and a bit more room to wiggle with some additional aesthetic changes, if you want.