我正在尝试从以下数据集中绘制点和线。
dados = structure(list(
Vertices = c(0.5, 1, 1.5, 2, 2.5, 3),
AAA = c(1.8156, 2.2355, 2.4784, 2.6283, 2.7266, 2.7947),
BBB = c(1.9603, 2.3802, 2.6231, 2.773, 2.8713, 2.9394),
CCC = c(2.1559, 2.5758, 2.8188, 2.9686, 3.0669, 3.135)),
row.names = c(NA, 6L), class = "data.frame")
ggplot(data = dados, aes(x = Vertices)) +
#lower points
geom_point(aes(y = AAA, color = "purple")) +
geom_line(aes(y = AAA, color = "purple", linetype ="longdash")) +
#intermediate points
geom_point(aes(y = BBB, color = "blue")) +
geom_line(aes(y = BBB, color="blue", linetype = "dotted")) +
#higher points
geom_point(aes(y = CCC, color = "green")) +
geom_line(aes(y = CCC, color="green", linetype = "dashed")) +
scale_color_manual(values = c("purple", "blue", "green"),
labels = c("AAA","BBB","CCC")) +
ggtitle("Curvas Spread Debentures") +
xlab("Years") + ylab("% points")
#helps visualize numerically
dados
总结:美学颜色不符合代码顺序
“ AAA”是最小值,应该是紫色,但是以绿色绘制,并且在图例中被指定为“ CCC”。
“ BBB”是中间值,应该是蓝色,但是以紫色绘制,并在图例中分配为“ AAA”
“ CCC”是最大值,应该是绿色,但以蓝色绘制,在图例中被指定为“ BBB”
为了简化起见,我还尝试仅绘制点或仅绘制线,但是颜色和图例也被弄乱了。 这是一个非常简单的代码,完全出错了,我没看到错误,有什么想法吗?
答案 0 :(得分:1)
您是想做这样的事情吗?
library(ggplot2)
tidyr::pivot_longer(dados, cols = -Vertices) %>%
ggplot() + aes(Vertices, value, color = name) +
geom_point() + geom_line(aes(linetype=name)) +
ggtitle("Curvas Spread Debentures") +
xlab("Years") + ylab("% points") +
scale_color_manual(values = c('purple', 'blue', 'green'))