我正在尝试绘制带有两个y轴的图形。我知道还有很多与此类似的问题,但我似乎无法根据其他帖子来弄清楚
所以我遇到的问题是y轴刻度。这是我在做什么
Time <- c("June-2018-30", "July-2018-31", "August-2018-31", "September-2018-30",
"October-2018-31", "November-2018-30", "December-2018-31", "January-2019-31",
"February-2019-28", "March-2019-31", "April-2019-30", "May-2019-31")
Bitcoin <- c(3.469861e-17, 3.188903e-17, 2.685114e-17, 2.42335e-17, 2.322641e-17,
2.447058e-17, 3.18029e-17, 2.944836e-17, 2.839419e-17, 2.76008e-17,
2.661607e-17, 2.536966e-17)
`USD Return` <- c(2.35e-13, 2.27e-13, 1.80e-13, 1.60e-13, 1.51e-13, 1.33e-13, 1.18e-13,
1.08e-13, 1.047e-13, 1.09e-13, 1.37e-13, 1.83e-13)
total.values3 <- data.frame(Time, Bitcoin,`USD Return`, stringsAsFactors = F)
library(ggplot2)
ggplot(data=total.values3, aes(x=Time, y=`USD Return`, group=1)) +
geom_line(aes(y = `USD Return`), color = "blue") +
geom_line(aes(y = Bitcoin), color = "red") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous("USD Return",
sec.axis = sec_axis(~./10000, name = "Bitcoin Return")) +
scale_x_date(labels=date_format("%B-%Y-%d"),
date_labels = "%B-%Y", breaks = total.values3$Time)
这是输出内容的图片
我不确定出了什么问题。我可以看到比例尺是错误的。我不明白为什么比特币线只是一条直线。我也不知道为什么右边的y轴变成负数
答案 0 :(得分:1)
total.values3$Time <- as.Date(total.values3$Time, format = "%B-%Y-%d")
ggplot(data=total.values3, aes(x=Time, group=1)) +
geom_line(aes(y = `USD Return`), color = "blue") +
geom_line(aes(y = Bitcoin*10000), color = "red") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous("USD Return",
sec.axis = sec_axis(~./10000, name = "Bitcoin Return")) +
scale_x_date(labels=date_format("%B-%Y-%d"),
date_labels = "%B-%Y", breaks = total.values3$Time)
这应该可以解决问题。