I have the following data set.
> y
DF GE
2006-12-21 -0.0004659447 -0.009960682
2006-12-22 -0.0009323238 -0.005295208
2006-12-26 -0.0032661785 0.003726377
2006-12-27 0.0042131332 0.002121453
2006-12-28 -0.0009323238 -0.008203228
2006-12-29 -0.0135313109 -0.007203842
When I fortify it becomes this.
> fortify(y, melt=TRUE)
Index Series Value
1 2006-12-21 DF -0.0004659447
2 2006-12-22 DF -0.0009323238
3 2006-12-26 DF -0.0032661785
4 2006-12-27 DF 0.0042131332
5 2006-12-28 DF -0.0009323238
6 2006-12-29 DF -0.0135313109
7 2006-12-21 GE -0.0099606815
8 2006-12-22 GE -0.0052952078
9 2006-12-26 GE 0.0037263774
10 2006-12-27 GE 0.0021214532
11 2006-12-28 GE -0.0082032284
12 2006-12-29 GE -0.0072038420
Now I would like to run a scatterplot in ggplot2. At the moment, the function I use is the following.
x <- fortify(x, melt=TRUE)
ggplot(data=x) +
geom_point(size=10, aes(x=Series, y=Value, colour=Index))
But what I really want is a scatter plot with the values of DF and GE to be the coordinates on X and Y axis and for the date to be shown by colour.
I simply cannot work out how to amend the data structure to achieve this.
Do you mean something like this?
library(tidyverse)
df %>%
rownames_to_column("date") %>%
mutate(date = as.Date(date)) %>%
ggplot(aes(x = DF, y = GE)) +
geom_point(aes(colour = date))
df <- read.table(text =
" DF GE
2006-12-21 -0.0004659447 -0.009960682
2006-12-22 -0.0009323238 -0.005295208
2006-12-26 -0.0032661785 0.003726377
2006-12-27 0.0042131332 0.002121453
2006-12-28 -0.0009323238 -0.008203228
2006-12-29 -0.0135313109 -0.007203842", header = T)
I wouldn't use fortify()
. I would keep your data the way it is and do something like:
library(tidyverse)
y %>%
rownames_to_column() %>%
ggplot(aes(x = DF, y = GE, color = index) +
geom_point()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.