AK88
AK88

Reputation: 3026

ggplot specifying x and y correctly

I got data which looks like this:

        ISIN 2016-01-05 2016-01-11 2016-01-18 2016-01-25 2016-02-01 2016-02-08 2016-02-15 2016-02-22 2016-02-29 2016-03-09 2016-03-14 2016-03-24
1 KZK2KY030871    91.1165    91.2097    91.3173    91.2241    91.4101    91.0851    91.0979    89.6833    89.4669    89.5396    90.4002    90.4249
2 KZK2KY050443    89.9079    90.0086    90.1254    90.0408    90.2386    89.9094    89.9315    88.4538    88.2538     88.345    89.2652     89.305
3 KZK2KY050450    91.1218    91.2063    91.3048    91.1968     91.388    91.0072    91.0096    89.3449    89.1223    89.1887    90.2121    90.2334
4 KZK2KY050468     90.957    91.0395    91.1362    91.0248    91.2197    90.8183    90.8187      89.05    88.8253    88.8884      89.98    89.9995
5 KZK2KY050476    88.7343    88.7973    88.8785    88.7521    89.0199    88.3094     88.311    84.7608    84.5707    84.6103    86.8479    86.8885
6 KZK2KY050484    89.8963    89.9224    89.9707    89.8465     90.168    89.1252    89.1378    83.4505    83.3609    83.3898    86.7662    86.8651

Names of a product under ISIN and each products' value for a given date in columns.

I would like to plot them all in one graph - there will be multiply line and ideally I would like to single out with glowing color some of the products. I tried to 'melt' the data and got the following structure:

      ISIN      variable    value
1  KZK2KY030871 2016-01-05 91.1165
2  KZK2KY050443 2016-01-05 89.9079
3  KZK2KY050450 2016-01-05 91.1218
4  KZK2KY050468 2016-01-05  90.957
5  KZK2KY050476 2016-01-05 88.7343
6  KZK2KY050484 2016-01-05 89.8963
7  KZK2KY050492 2016-01-05 89.8838
8  KZKDKY060074 2016-01-05  90.334
9  KZKDKY060082 2016-01-05  87.412
10 KZKDKY060090 2016-01-05 86.9471

And tried to plot a graph with ggplot:

ggplot(BDmelt, aes(x = variable, y = ISIN, color = ISIN)) +
  theme_bw() +
  theme(legend.position="none") +
  geom_line()

What I get here is - no lines at all. I see each ISIN along y-axis. My x-axis values are so cramped - they are just a black line. I would like to get a plot with all the lines (y-axis values vary from 0 to 150) and also be able to have a meaningful step size for x-axis so that I could see the dates. Please help.

Adding this bit made it more or less tolerable: theme(axis.text.x = element_text(angle = 90, hjust = 1), axis.text = element_text(size=5)) +

Upvotes: 0

Views: 2413

Answers (1)

neilfws
neilfws

Reputation: 33802

Your first issue is that ISIN is not numeric, so doesn't make a great y-axis variable: you need to use "value". And your second issue is that you need to group and color by ISIN. Something like this should get you started:

ggplot(BDmelt, aes(variable, value)) + 
  geom_line(aes(color = ISIN, group = ISIN)) + 
  theme_bw() +
  theme(legend.position = "none")

enter image description here

You probably also want to convert the "variable" column to date format, so as you can use scale_x_date to format the x-axis.

Upvotes: 2

Related Questions