R ggplot2 line graph adding fill when I don't want it - r

I've got a dataset with some velocity data. I have an extra column in this dataset called followtime that corresponds to certain values in velocity, and highlights them as a factor (ie, certain values in trial 1 will be highlighted in followtime with 1 and the rest 0, certain values in trial 2 will be highlighted with 2 and the rest 0, etc.). See below (this example has random velocities, but followtime is looks pretty much like it does in my own dataset).
trial <- c(rep(1,25), rep(2, 25), rep(3, 25))
minitime <- c(1:25)
time <- c(rep(minitime, 3))
totalsmooth_velocity <- runif(75, min=-3, max=2)
followtime <- c(rep(0, 10), rep(1, 10), rep(0,5), rep(0, 5), rep(2, 5), rep(0, 15), rep(0, 15), rep(3, 10))
df <- cbind(trial, time, totalsmooth_velocity, followtime)
df <- as.data.frame(df)
df$time <- as.integer(df$time)
I'd like to make a line graph with ggplot2 that color-codes each followtime in a different color. Here's my data graphed with a scatter plot (which for some reason has no trouble with this):
With corresponding code:
stim1bfollows<- ggplot()+
geom_point(data=df, aes(x=time, y=totalsmooth_velocity, color = as.factor(followtime)), size = 1.0)+
geom_hline(yintercept=c(0, -0.16))
stim1bfollows
When I try to code this as a line graph, however, it looks like this:
With corresponding code:
stim1bfollows<- ggplot()+
geom_line(data=df, aes(x=time, y=totalsmooth_velocity, color = as.factor(followtime)), size = 1.0)+
geom_hline(yintercept=c(0, -0.16))
stim1bfollows
I don't want that fill! I'm not sure what's going wrong, I've tried a couple of changes with 'dodge' and treated color as both a factor and as numerically but if anybody could point me in the right direction I'd be extremely grateful. Thank you!
EDIT
Having this issue even with group! Image:
stim1bfollows<- ggplot()+
geom_line(data=follows, aes(x=time, y=totalsmooth_velocity, group = as.factor(followtime), color = as.factor(followtime)), size = 1.0)+
geom_hline(yintercept=c(0, -0.16))
stim1bfollows
Also for reference, my str(follows) of the original dataset:
str(follows)
'data.frame': 750 obs. of 13 variables:
$ bartrial : int 9 9 9 9 9 9 9 9 9 9 ...
$ trial : int 1 1 1 1 1 1 1 1 1 1 ...
$ time : int 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 ...
$ X : num 158 158 158 158 158 ...
$ Y : num 64.5 64.6 64.6 64.5 64.5 ...
$ velocity : num 0.05766 -0.0266 -0.05106 -0.00543 0.04506 ...
$ barvelocity : num -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 ...
$ index : num -0.3604 0.1663 0.3191 0.0339 -0.2816 ...
$ veldiff : num 0.218 0.133 0.109 0.155 0.205 ...
$ direction : logi TRUE TRUE TRUE TRUE TRUE TRUE ...
$ response : int 0 1 1 1 0 0 0 0 1 0 ...
$ totalsmooth_velocity: num 0.0173 0.0185 0.0202 0.0233 0.0272 ...
$ followtime : num 0 0 0 0 0 0 0 0 0 0 ...
And the head:
head(follows)
bartrial trial time X Y velocity barvelocity index veldiff direction response
2001 9 1 17026 158.2507 64.52043 0.057657143 -0.16 -0.36035714 0.2176571 TRUE 0
2002 9 1 17027 158.1855 64.57809 -0.026600000 -0.16 0.16625000 0.1334000 TRUE 1
2003 9 1 17028 158.2674 64.55149 -0.051057143 -0.16 0.31910714 0.1089429 TRUE 1
2004 9 1 17029 158.2733 64.50043 -0.005428571 -0.16 0.03392857 0.1545714 TRUE 1
2005 9 1 17030 158.2763 64.49500 0.045057143 -0.16 -0.28160714 0.2050571 TRUE 0
2006 9 1 17031 158.2363 64.54006 0.028971429 -0.16 -0.18107143 0.1889714 TRUE 0
totalsmooth_velocity followtime
2001 0.01732903 0
2002 0.01852428 0
2003 0.02024635 0
2004 0.02326663 0
2005 0.02719260 0
2006 0.03045590 0
Adding a small subset:
dput(subset(follows, time %in% 17100:17130))
structure(list(bartrial = c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L), trial = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), time = c(17100L,
17101L, 17102L, 17103L, 17104L, 17105L, 17106L, 17107L, 17108L,
17109L, 17110L, 17111L, 17112L, 17113L, 17114L, 17115L, 17116L,
17117L, 17118L, 17119L, 17120L, 17121L, 17122L, 17123L, 17124L,
17125L, 17126L, 17127L, 17128L, 17129L, 17130L, 17100L, 17101L,
17102L, 17103L, 17104L, 17105L, 17106L, 17107L, 17108L, 17109L,
17110L, 17111L, 17112L, 17113L, 17114L, 17115L, 17116L, 17117L,
17118L, 17119L, 17120L, 17121L, 17122L, 17123L, 17124L, 17125L,
17126L, 17127L, 17128L, 17129L, 17130L, 17100L, 17101L, 17102L,
17103L, 17104L, 17105L, 17106L, 17107L, 17108L, 17109L, 17110L,
17111L, 17112L, 17113L, 17114L, 17115L, 17116L, 17117L, 17118L,
17119L, 17120L, 17121L, 17122L, 17123L, 17124L, 17125L, 17126L,
17127L, 17128L, 17129L, 17130L), X = c(158.554971428571, 158.561857142857,
158.545942857143, 158.442742857143, 158.463457142857, 158.447628571429,
158.4628, 158.426028571429, 158.3998, 158.355114285714, 158.339971428571,
158.318457142857, 158.339657142857, 158.298142857143, 158.241714285714,
158.2846, 158.311114285714, 158.324142857143, 158.307228571429,
158.255428571429, 158.271914285714, 158.308028571429, 158.2894,
158.301342857143, 158.308428571429, 158.227228571429, 158.250057142857,
158.226771428571, 158.174914285714, 158.200485714286, 158.213085714286,
98.11471429, 99.706, 101.0531143, 102.1066286, 103.2292, 103.9274,
104.7769429, 105.7868571, 106.5872857, 107.3484286, 108.6168571,
109.3342286, 110.2153714, 111.2181714, 112.8689429, 114.7111143,
116.7568571, 118.523, 119.8732857, 121.4064, 122.5118286, 123.6406286,
124.6844, 125.5278286, 126.3410286, 128.1753143, 129.8935429,
131.1022857, 132.2688286, 133.3624571, 133.9324286, 96.6617714285714,
96.6587142857143, 96.1991428571429, 94.7394285714286, 92.9670285714286,
90.7313428571429, 88.4762, 85.5486571428571, 82.0275142857143,
79.3814, 77.4777428571429, 75.5628857142857, 72.7176, 69.8095142857143,
66.4666571428571, 61.6254, 57.966, 54.3682571428571, 51.6569428571429,
50.0204, 48.7530285714286, 47.9830571428571, 46.7885142857143,
45.3995428571429, 43.5878, 41.7556285714286, 39.6544571428571,
37.6700285714286, 35.0251714285714, 32.6265714285714, 29.3777142857143
), Y = c(57.2500571428571, 57.0420857142857, 56.7889714285714,
56.6496857142857, 56.5398571428571, 56.4711714285714, 56.2698285714286,
56.1793428571429, 56.0277714285714, 56.0177142857143, 55.7782857142857,
55.6382, 55.7419714285714, 55.6086857142857, 55.5973142857143,
55.6697428571429, 55.5047714285714, 55.4622571428571, 55.1674285714286,
55.1252857142857, 54.8758857142857, 54.8422571428571, 54.7050857142857,
54.6246571428571, 54.6242285714286, 54.5318, 54.3272285714286,
54.1329428571429, 54.1315428571429, 54.0057142857143, 53.9648857142857,
189.6266286, 189.5072571, 189.4862571, 189.3138, 189.2692571,
189.2216857, 189.1862857, 189.1338, 189.0529143, 188.9495429,
188.8440286, 188.7655143, 188.7224286, 188.5904857, 188.5451429,
188.5732286, 188.3531429, 188.1283429, 187.9746571, 187.5346,
187.2002857, 186.9196571, 186.6167429, 186.5036857, 186.2668,
185.873, 185.4187714, 185.1869429, 184.8553429, 184.6551429,
184.5514857, 10.31026, 10.3998028571429, 9.98941714285714, 9.93686571428571,
10.0085457142857, 10.1963542857143, 10.3918028571429, 10.4927542857143,
10.7132285714286, 10.9808628571429, 11.2396571428571, 11.2958885714286,
11.4141742857143, 11.5568771428571, 11.7057142857143, 12.0326514285714,
12.2709742857143, 12.49016, 12.7785485714286, 13.0098257142857,
13.2676714285714, 13.3456028571429, 13.3960371428571, 13.4590771428571,
13.46362, 13.5725371428571, 13.756, 13.8612657142857, 13.9868371428571,
14.1618342857143, 14.4414857142857), velocity = c(-0.207971428571433,
-0.253114285714283, -0.139285714285712, -0.109828571428572, -0.0686857142857136,
-0.201342857142862, -0.0904857142857125, -0.15157142857143, -0.0100571428571357,
-0.239428571428583, -0.140085714285711, 0.103771428571434, -0.133285714285712,
-0.0113714285714366, 0.0724285714285742, -0.164971428571434,
-0.0425142857142831, -0.294828571428567, -0.0421428571428564,
-0.249400000000009, -0.0336285714285651, -0.137171428571428,
-0.0804285714285768, -0.000428571428571445, -0.0924285714285702,
-0.204571428571427, -0.194285714285712, -0.00140000000000384,
-0.125828571428563, -0.0408285714285697, -0.104885714285722,
-0.119371429, -0.021, -0.172457143, -0.044542857, -0.047571429,
-0.0354, -0.052485714, -0.080885714, -0.103371429, -0.105514286,
-0.078514286, -0.043085714, -0.131942857, -0.045342857, 0.028085714,
-0.220085714, -0.2248, -0.153685714, -0.440057143, -0.334314286,
-0.280628571, -0.302914286, -0.113057143, -0.236885714, -0.3938,
-0.454228571, -0.231828571, -0.3316, -0.2002, -0.103657143, -0.136857143,
0.0895428571428578, -0.410385714285713, -0.0525514285714301,
0.0716800000000006, 0.187808571428572, 0.195448571428571, 0.100951428571427,
0.220474285714285, 0.267634285714285, 0.258794285714286, 0.0562314285714294,
0.118285714285713, 0.142702857142858, 0.148837142857142, 0.326937142857144,
0.238322857142858, 0.219185714285715, 0.28838857142857, 0.231277142857143,
0.257845714285715, 0.0779314285714268, 0.050434285714287, 0.0630399999999991,
0.00454285714285874, 0.108917142857143, 0.183462857142857, 0.105265714285713,
0.12557142857143, 0.174997142857144, 0.279651428571428, 0.17196
), barvelocity = c(-0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16), index = c(1.29982142857146,
1.58196428571427, 0.870535714285703, 0.686428571428577, 0.42928571428571,
1.25839285714289, 0.565535714285703, 0.947321428571435, 0.0628571428570979,
1.49642857142864, 0.875535714285691, -0.648571428571465, 0.833035714285701,
0.0710714285714786, -0.452678571428589, 1.03107142857146, 0.265714285714269,
1.84267857142855, 0.263392857142852, 1.55875000000005, 0.210178571428532,
0.857321428571423, 0.502678571428605, 0.00267857142857153, 0.577678571428564,
1.27857142857142, 1.2142857142857, 0.00875000000002402, 0.786428571428521,
0.25517857142856, 0.65553571428576, 0.746071429, 0.13125, 1.077857143,
0.278392857, 0.297321429, 0.22125, 0.328035714, 0.505535714,
0.646071429, 0.659464286, 0.490714286, 0.269285714, 0.824642857,
0.283392857, -0.175535714, 1.375535714, 1.405, 0.960535714, 2.750357143,
2.089464286, 1.753928571, 1.893214286, 0.706607143, 1.480535714,
2.46125, 2.838928571, 1.448928571, 2.0725, 1.25125, 0.647857143,
0.855357143, -0.559642857142861, 2.56491071428571, 0.328446428571438,
-0.448000000000004, -1.17380357142858, -1.22155357142857, -0.630946428571422,
-1.37796428571428, -1.67271428571428, -1.61746428571429, -0.351446428571434,
-0.739285714285709, -0.891892857142862, -0.930232142857135, -2.04335714285715,
-1.48951785714286, -1.36991071428572, -1.80242857142856, -1.44548214285715,
-1.61153571428572, -0.487071428571417, -0.315214285714294, -0.393999999999994,
-0.0283928571428671, -0.680732142857143, -1.14664285714285, -0.657910714285703,
-0.784821428571436, -1.09373214285715, -1.74782142857143, -1.07475
), veldiff = c(-0.0479714285714331, -0.0931142857142825, 0.0207142857142875,
0.0501714285714277, 0.0913142857142864, -0.0413428571428619,
0.0695142857142875, 0.00842857142857042, 0.149942857142864, -0.0794285714285829,
0.0199142857142894, 0.263771428571434, 0.0267142857142878, 0.148628571428563,
0.232428571428574, -0.00497142857143387, 0.117485714285717, -0.134828571428567,
0.117857142857144, -0.0894000000000085, 0.126371428571435, 0.0228285714285724,
0.0795714285714232, 0.159571428571429, 0.0675714285714298, -0.0445714285714268,
-0.0342857142857122, 0.158599999999996, 0.0341714285714366, 0.11917142857143,
0.0551142857142784, 0.040628571, 0.139, -0.012457143, 0.115457143,
0.112428571, 0.1246, 0.107514286, 0.079114286, 0.056628571, 0.054485714,
0.081485714, 0.116914286, 0.028057143, 0.114657143, 0.188085714,
-0.060085714, -0.0648, 0.006314286, -0.280057143, -0.174314286,
-0.120628571, -0.142914286, 0.046942857, -0.076885714, -0.2338,
-0.294228571, -0.071828571, -0.1716, -0.0402, 0.056342857, 0.023142857,
0.249542857142858, -0.250385714285713, 0.10744857142857, 0.231680000000001,
0.347808571428572, 0.355448571428571, 0.260951428571428, 0.380474285714285,
0.427634285714285, 0.418794285714286, 0.216231428571429, 0.278285714285713,
0.302702857142858, 0.308837142857142, 0.486937142857144, 0.398322857142858,
0.379185714285715, 0.44838857142857, 0.391277142857143, 0.417845714285715,
0.237931428571427, 0.210434285714287, 0.223039999999999, 0.164542857142859,
0.268917142857143, 0.343462857142857, 0.265265714285713, 0.28557142857143,
0.334997142857144, 0.439651428571428, 0.33196), direction = c(TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE), response = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), totalsmooth_velocity = c(-0.185715482616494,
-0.169799554542362, -0.159003977144789, -0.148207077837678, -0.137191691887319,
-0.128149664364088, -0.119572837388017, -0.109953053079138, -0.0972978435598013,
-0.0831080759384155, -0.071115594814103, -0.0650522447859859,
-0.0666755590279073, -0.0730178458107374, -0.0807231123791393,
-0.0864353659777761, -0.093731176054967, -0.104374785327469,
-0.112291710942601, -0.11532488840979, -0.115998870083849, -0.115261598380177,
-0.114061015714174, -0.112121981406158, -0.109279350709846, -0.106646059940738,
-0.105335045414329, -0.10502031419553, -0.104660283980211, -0.104294507030314,
-0.103962535607781, -0.0544997541294192, -0.0759672933472838,
-0.0928318384423578, -0.0995235408007862, -0.0921496441281631,
-0.0808943187101956, -0.075941734832591, -0.0721387042961063,
-0.0644417616889991, -0.0631996614782276, -0.0701231877757797,
-0.080159555991031, -0.0935121675370214, -0.110384423826791,
-0.134341886011727, -0.164771274930442, -0.19560773152681, -0.220786396744707,
-0.24470800956836, -0.271709665272387, -0.295631039236247, -0.3103118068394,
-0.311136008145884, -0.303141587685218, -0.294649077535086, -0.287800911512628,
-0.278977437979406, -0.26785606082973, -0.254114183957905, -0.228672234833075,
-0.193945588974598, -0.0248176049007914, -0.0280154922261918,
-0.00509363044425982, 0.0359756870492962, 0.0802930373308052,
0.112958997476596, 0.13498042401608, 0.156286940578198, 0.175363274888579,
0.190694154672854, 0.197511866511148, 0.197482141367721, 0.198739873544398,
0.206075849950098, 0.215615753800187, 0.222496823856169, 0.221856298879546,
0.209154962712217, 0.189688538371653, 0.173346673382715, 0.154502814473438,
0.127758414088336, 0.103345128326991, 0.091494613288984, 0.0964662157108718,
0.11027334789746, 0.124778764300703, 0.146992697502439, 0.180840414940991,
0.213557223353035, 0.232378429475247), followtime = c(0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3)), row.names = c("2075", "2076", "2077", "2078",
"2079", "2080", "2081", "2082", "2083", "2084", "2085", "2086",
"2087", "2088", "2089", "2090", "2091", "2092", "2093", "2094",
"2095", "2096", "2097", "2098", "2099", "2100", "2101", "2102",
"2103", "2104", "2105", "20751", "20761", "20771", "20781", "20791",
"20801", "20811", "20821", "20831", "20841", "20851", "20861",
"20871", "20881", "20891", "20901", "20911", "20921", "20931",
"20941", "20951", "20961", "20971", "20981", "20991", "21001",
"21011", "21021", "21031", "21041", "21051", "20752", "20762",
"20772", "20782", "20792", "20802", "20812", "20822", "20832",
"20842", "20852", "20862", "20872", "20882", "20892", "20902",
"20912", "20922", "20932", "20942", "20952", "20962", "20972",
"20982", "20992", "21002", "21012", "21022", "21032", "21042",
"21052"), class = "data.frame")

You need to use groupin your aes for geom_line:
ggplot(data=df, aes(x=time, y=totalsmooth_velocity, group = as.factor(followtime), color = as.factor(followtime)))+
geom_point( size = 1.0)+
geom_hline(yintercept=c(0, -0.16), color = "black")+
geom_line()

Ok, after several iterations, I did need to include an aes(group) value. However, in this case, I grouped by the original trials, instead of the followtimes. Ggplot2 was treating all followtimes=0 as the same group. New code:
stim1bfollows<- ggplot(data=follows, aes(x=time, y=totalsmooth_velocity, group=as.factor(trial), color=as.factor(followtime)))+
geom_line(size = 1.0)+
geom_point(size=1.5)+
geom_hline(yintercept=c(0,-0.16), color = c("black", "red"))+scale_color_manual(values = c("black", "coral", "aquamarine4", "violetred"))
stim1bfollows
And new graph (keeping the geom_point overlay over geom_line, makes the figure look really slick):
Thanks for the help.

Related

Different colors for every column in heat map using geom_tile()

I am trying to get a heat map where every column has a different color.
I have a heatmap like this:
# install.packages("reshape")
library(reshape)
library(ggplot2)
# Data
set.seed(8)
m <- matrix(round(rnorm(200), 2), 5, 5)
colnames(m) <- paste("Row", 1:5)
rownames(m) <- paste("col", 1:5)
# long format
df <- melt(m)
colnames(df) <- c("x", "y", "value")
ggplot(df, aes(x = x, y = y, fill = value)) +
geom_tile()
I would like to get for each columun col1,col2,col3,col4, and col5 a different color.
For example:
For col1 blue, col2 2 green, violet for col3, yellow for col4 and orange in col5.
I need to catch these ideas because I am doing the next plot with the next dataset:
dput(bdd)
structure(list(var = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L), .Label = c("var_1", "var_2",
"var_3", "var_4", "var_5", "var_6", "var_7", "var_8", "var_9",
"var_10", "var_11", "var_12", "var_13", "var_14"), class = "factor"),
value = c(4.93, 2.85, 2.075, 1.91, 1.73, 1.34, 0.615, 0.145,
0.14, 0.11, 0.09, 0.06, 0.06, 0.015, 4.13, 1.65, 1.985, 0.51,
5.805, 0.84, 1.28, 0.03, 0.235, 0.145, 0.145, 0.205, 0.03,
0.2, 1.135, 2.175, 2.735, 1.69, 0.86, 0.715, 1.905, 0.17,
0.86, 0.055, 0.03, 0.075, 0.14, 0.005, 3.55, 4.225, 5.985,
0.185, 1.17, 0.91, 0.49, 1.34, 0.485, 0.1, 0.145, 1.145,
0.53, 0.11, 12.06, 1.995, 2.205, 0.48, 1.875, 2.03, 0.335,
0.26, 1.25, 0.225, 0.245, 0.52, 0.075, 0.04), country = structure(c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), .Label = character(0)),
country1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L), .Label = c("C1", "C2", "C3", "C4", "C5"), class = "factor")), row.names = c(NA,
-70L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(data=bdd,aes(x=country1,y=var,fill=value))+
geom_tile(aes(alpha=value,fill=country),color="white")+
geom_text(aes(label = sprintf("%0.3f", round(value, digits = 3))))+
scale_fill_gradient(low="white", high="blue")+
scale_alpha(range = c(0, 1))+
theme_classic()+theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=0,hjust=0.5,vjust=0.5), legend.position = "none")+
labs( fill="% ",y = "y ")
But what I need is every column with a different color as in the first example.
Best.
ggplot(data=bdd,aes(x=country1,y=var,fill=country1))+
geom_tile(aes(alpha=value),color="white")+
geom_text(aes(label = sprintf("%0.3f", round(value, digits = 3))))+
scale_alpha(range = c(0, 1))+
theme_classic()+theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=0,hjust=0.5,vjust=0.5), legend.position = "none")+
labs( fill="% ",y = "y ")
To specify the colors for each column to be different than the default spectrum, you could use one of the discrete fill options like scale_fill_discrete, scale_fill_manual, or a custom palette like ggthemes::scale_fill_tableau(palette = "Nuriel Stone")

ggplot2 - customize two-factor legend

I am using ggplot2 to plot monthly vertical profiles of soil moisture in two sites, for both observed and modeled data.
I am using interaction to add colours to both factors (month and type). I am also creating two different manual color palettes with the colors I need. This is how to to reproduce the plot:
library(ggplot2)
df1<- structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L), .Label = c("IL_Shabbona_5_NNE", "ME_Limestone_4_NNW",
"ME_Old_Town_2_W", "MI_Chatham_1_SE", "MI_Gaylord_9_SSW", "MN_Goodridge_12_NNW",
"MN_Sandstone_6_W", "NY_Ithaca_13_E", "NY_Millbrook_3_W", "WI_Necedah_5_WNW"
), class = "factor"), month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L), depth = c(5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100), value = c(0.38,
0.4, 0.37, 0.32, 0.29, 0.3, 0.24, 0.28, 0.24, 0.26, 0.32, 0.39,
0.13, NaN, 0.13, 0.12, 0.1, 0.1, 0.06, 0.07, 0.09, 0.1, 0.12,
0.13, 0.39, 0.39, 0.37, 0.35, 0.33, 0.31, 0.27, 0.29, 0.27, 0.28,
0.34, 0.38, 0.1, NaN, 0.12, 0.11, 0.09, 0.09, 0.05, 0.06, 0.09,
0.09, 0.11, 0.11, 0.39, 0.41, 0.38, 0.35, 0.34, 0.32, 0.29, 0.33,
0.31, 0.3, 0.34, 0.36, 0.1, NaN, 0.1, 0.1, 0.09, 0.08, 0.05,
0.05, 0.08, 0.08, 0.1, 0.1, 0.32, 0.31, 0.33, 0.34, 0.36, 0.34,
0.29, 0.33, 0.32, 0.31, 0.32, 0.33, 0.06, 0.06, 0.07, 0.06, 0.06,
0.05, 0.03, 0.03, 0.04, 0.05, 0.06, 0.06, 0.4, 0.4, 0.41, 0.41,
0.45, 0.47, 0.43, 0.4, 0.39, 0.38, 0.38, 0.4, 0.05, 0.05, 0.05,
0.06, 0.05, 0.05, 0.04, 0.04, 0.05, 0.05, 0.06, 0.05, 0.35, 0.35,
0.36, 0.33, 0.29, 0.28, 0.27, 0.26, 0.26, 0.28, 0.3, 0.36, 0.35,
0.35, 0.36, 0.33, 0.29, 0.28, 0.27, 0.27, 0.27, 0.28, 0.3, 0.35,
0.34, 0.35, 0.35, 0.34, 0.3, 0.29, 0.28, 0.28, 0.28, 0.29, 0.3,
0.34, 0.28, 0.29, 0.3, 0.32, 0.31, 0.3, 0.29, 0.29, 0.29, 0.3,
0.3, 0.29, 0.26, 0.27, 0.27, 0.29, 0.29, 0.29, 0.28, 0.28, 0.28,
0.29, 0.29, 0.28, 0.38, 0.38, 0.39, 0.38, 0.31, 0.3, 0.29, 0.29,
0.3, 0.31, 0.35, 0.39, 0.36, 0.36, 0.37, 0.37, 0.31, 0.31, 0.29,
0.3, 0.3, 0.31, 0.33, 0.37, 0.37, 0.37, 0.37, 0.38, 0.32, 0.32,
0.31, 0.31, 0.31, 0.32, 0.33, 0.37, 0.31, 0.32, 0.32, 0.34, 0.33,
0.32, 0.31, 0.31, 0.32, 0.32, 0.31, 0.3, 0.27, 0.28, 0.28, 0.29,
0.31, 0.3, 0.3, 0.29, 0.3, 0.3, 0.3, 0.28), type = rep(c("observed","modeled"), each=120)), class = "data.frame", row.names = c(NA,
-240L))
# Create blue and red palettes
mypal.blue <- colorRampPalette(RColorBrewer::brewer.pal(6,"PuBu"))
mypal.red <- colorRampPalette(RColorBrewer::brewer.pal(6,"YlOrRd"))
# Plot
ggplot(df1, aes(x=value, y=-depth, colour=interaction(as.factor(month),type))) +
geom_path(size=1) + geom_point(size=0.7) +
facet_wrap(~ site, nrow=3) +
theme_bw(base_size=20) +
scale_colour_manual(values=c(mypal.blue(12),mypal.red(12))) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.title=element_blank()) + theme(legend.position = c(0.75, 0.13))
However, the legend is a complete mess.
I would like to create two separate legends, loosely based on this example.
one showing orange for observed and blue for modeled
the other one showing the actual color gradients and the months (ideally with the first letter instead of numbers)
How to create such legends?
Updated Answer
It just hit me that there is a relatively straightforward way to hack the legend to get pretty close to what you want. We relabel the legend labels and add a title. The hacky part is that you have to fiddle with the legend title spacing, legend key width, and text size to get the titles lined up over the legend keys.
With all those lines and colors and the complicated legend, the plot seems very busy and difficult to interpret beyond showing that the model doesn't fit the data very well, so maybe it would still be better to consider one of the other options in my or #neilfws's answer. In addition, because the legend title is manually hardcoded, it's not linked to the aesthetic mapping and you therefore have to be careful that "Modeled" and "Observed" are in the right order above the legend keys.
ggplot(df1, aes(x=value, y=-depth, colour=interaction(as.factor(month),type))) +
geom_path(size=1) + geom_point(size=0.7) +
facet_wrap(~ site, nrow=3) +
theme_bw(base_size=20) +
scale_colour_manual(values=c(mypal.blue(12),mypal.red(12)),
labels=rep(month.abb, 2)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.title=element_text(size=rel(0.6)),
legend.text=element_text(size=rel(0.5)),
legend.key.width=unit(1.1,"cm")) +
labs(colour="Modeled Observed")
Original Answer
AFAIK, there's no way to generate two separate legends for a single aesthetic within the normal ggplot workflow. In this case, that means you can have only a single color legend. Probably you could hack two different color legends by manipulating the underlying ggplot grob structure.
Another option would be to use two different aesthetics. The example below uses linetype to distinguish modeled and observed, but it doesn't provide as much constrast as the two different color sets.
library(tidyverse)
ggplot(df1 %>%
mutate(month=factor(month.abb[month], levels=month.abb)),
aes(x=value, y=-depth, linetype=type, colour=month)) +
geom_path(size=1) + geom_point(size=0.7) +
facet_wrap(~ site, nrow=3) +
theme_bw(base_size=20) +
scale_colour_manual(values=mypal.red(12)) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.title=element_blank())
For reference, here's what your original code produces (minus the change in legend position):
Another option would be to facet by month in addition to type. This takes up more space, but makes it easier to see both the month trend and the difference between modeled and observed.
ggplot(df1 %>%
mutate(month=factor(month.abb[month], levels=month.abb)),
aes(x=value, y=-depth, colour=type)) +
geom_path(size=1) + geom_point(size=0.7) +
facet_grid(month ~ site) +
theme_classic() +
theme(panel.background=element_rect(colour="grey50", fill=NA))
Looking at your data, it seems to me that what you want to visualize can be expressed something like this:
"How do observed values compare to modelled values at different depths, for each site, through time?"
So I would approach the chart differently: plot value versus month, color by type and use facets for site and depth.
library(tidyverse)
df1 %>%
mutate(Month = factor(month.abb[month],
levels = month.abb)) %>%
ggplot(aes(Month, value)) +
geom_point(aes(color = type)) +
facet_grid(depth~site) +
theme_bw()
It's now immediately apparent that the modeled values for site IL_Shabbona_5_NNE are closer to the observed, and more so at shallower depth.

R plot color legend by factor

Using R 3.3.1 in Windows 10. I'm making an x-y plot from 95 rows of data. The data are in 6 different groupings (a factor called "group"). The plot itself is easy enough, but I can't get the legend to properly account for the factor and color correctly.
Here's the data in a variable v1:
v1 <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("F9", "T26", "W37",
"W40", "W41", "W42"), class = "factor"), point = c(1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L), x = c(-7.064, -5.1681,
-6.4866, -2.7522, -4.6305, -4.2957, -3.7552, -4.9482, -5.6452,
-6.0302, -5.3244, -3.9819, -3.8123, -5.3085, -5.6096, -6.4557,
-5.2549, -3.4893, -3.5909, -2.5546, -3.7247, -5.1733, -3.3451,
-2.8993, -2.6835, -3.9495, -4.9649, -2.8438, -4.6926, -3.4768,
-3.1221, -4.8175, -4.5641, -3.549, -3.08, -2.4153, -2.9882, -3.4045,
-4.6394, -3.3404, -2.6728, -3.3517, -2.6098, -3.7733, -4.051,
-2.9385, -4.5024, -4.59, -4.5617, -4.0658, -2.4986, -3.7559,
-4.245, -4.8045, -4.6615, -4.0696, -4.6638, -4.6505, -3.7978,
-4.5649, -5.7669, -4.519, -3.8561, -3.779, -3.0549, -3.1241,
-2.1423, -3.2759, -4.224, -4.028, -3.3412, -2.8832, -3.3866,
-0.1852, -3.3763, -4.317, -5.3607, -3.3398, -1.9087, -4.431,
-3.7535, -3.2545, -0.806, -3.1419, -3.7269, -3.4853, -4.3129,
-2.8891, -3.0572, -5.3309, -2.5837, -4.1128, -4.6631, -3.4695,
-4.1045), y = c(7.76, 0.72, 4.1, 1.36, 0.13, -0.02, 0.13, 0.42,
1.49, 2.64, 1.01, 0.08, 0.22, 1.01, 1.53, 4.39, 0.99, 0.56, 0.43,
2.31, 0.31, 0.59, 0.62, 1.65, 2.12, 0.1, 0.24, 1.68, 0.09, 0.59,
1.23, 0.4, 0.36, 0.49, 1.41, 3.29, 1.22, 0.56, 0.1, 0.67, 2.38,
0.43, 1.56, 0.07, 0.08, 1.53, -0.01, 0.12, 0.1, 0.04, 3.42, 0.23,
0, 0.34, 0.15, 0.03, 0.19, 0.17, 0.2, 0.09, 2.3, 0.07, 0.15,
0.18, 1.07, 1.21, 3.4, 0.8, -0.04, 0.02, 0.74, 1.59, 0.71, 10.64,
0.64, -0.01, 1.06, 0.81, 4.58, 0.01, 0.14, 0.59, 7.35, 0.63,
0.17, 0.38, -0.08, 1.1, 0.89, 0.94, 1.52, 0.01, 0.1, 0.38, 0.02
)), .Names = c("group", "point", "x", "y"), class = "data.frame", row.names = c(NA,
-95L))
Here's the plot my attempts to overlay a legend:
> attach(v1)
> plot(x,y, pch=16, col=group) #simple plot, automatic colors
> #first legend
> legend("topleft", legend=group, pch=16, col=group)
> # colors matched, but it's breaking out every point
> legend("topright", legend=levels(group), pch=16, col=group)
> # Corrected the number of levels in legend, but no colors
>
You can see that the first legend appears correct color-wise, but it shows an entry for every point and runs out of space. The second legend shows group as factor levels, which is what I want, but it doesn't change the colors.
I realize that I could color as a vector (e.g. col(c("black","red", etc.), but since the original plot command automatically assigned colors, I'm looking to do it "automatically" in my legend and avoid the risk of putting the wrong colors in my vector.
Thanks!
base R solution:
attach(v1)
plot(x,y, pch=16, col=group)
legend("topleft", legend=levels(group), pch=16, col=unique(group))
ggplot2 solution
ggplot(v1)+
geom_point(aes(x=x,y=y,colour=group))+
theme_bw()
Again, I would strongly suggest the use of ggplot2 over base R unless you're only exploring the data. There are plenty of questions/answers on the matter on SO.
Try creating a new column in v1 that is a number based on the value of group (as a factor). Pass this column as the col when plotting the points. Then create a vector of numbers for legend in the same way and pass that as the col for legend.
v1$cols = as.numeric(as.factor(v1$group))
legend.cols = as.numeric(as.factor(levels(v1$group)))
plot(v1$x , v1$y, pch=16, col=v1$cols)
legend("topright", legend=levels(group), pch=16, col=legend.cols)

ggplot2 Error in eval(expr, envir, enclos) : object not found

Data Sets
> dput(head(spdistbc,50))
structure(list(Lane = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), Vehicle.class = c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
speedmph = c(0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2,
30.6, 34, 37.4, 40.8, 0, 3.4, 6.8, 10.2, 13.6, 17, 20.4,
23.8, 27.2, 30.6, 34, 37.4, 40.8, 3.4, 6.8, 10.2, 13.6, 17,
20.4, 23.8, 27.2, 30.6, 34, 37.4, 40.8, 0, 3.4, 6.8, 10.2,
13.6, 17, 20.4, 23.8, 27.2, 30.6, 34, 37.4), cprob = c(0,
0, 0.03, 0.06, 0.11, 0.2, 0.28, 0.43, 0.56, 0.75, 0.91, 0.97,
1, 0, 0, 0.01, 0.01, 0.02, 0.05, 0.17, 0.36, 0.57, 0.76,
0.93, 0.99, 1, 0, 0.01, 0.01, 0.04, 0.07, 0.16, 0.32, 0.55,
0.76, 0.94, 0.99, 1, 0, 0, 0, 0.01, 0.03, 0.06, 0.11, 0.25,
0.47, 0.74, 0.92, 0.98)), .Names = c("Lane", "Vehicle.class",
"speedmph", "cprob"), row.names = c(7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 52L, 53L, 66L, 67L, 68L, 69L, 70L, 71L, 72L,
73L, 74L, 75L, 76L, 77L), class = "data.frame")
> dput(head(cspdistbv,50))
structure(list(lanem = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L), cars = structure(c(34, 35, 36, 37, 38, 39, 40, 24,
26, 28, 30, 32, 34, 36, 38, 40, 20, 25, 30, 35, 40, 10, 15, 20,
25, 30, 35, 40, 10, 15, 20, 25, 30, 35, 40, 35, 40, 45, 50, 55,
0, 0.03, 0.07, 0.17, 0.67, 0.93, 1, 0, 0.03, 0.1, 0.1, 0.2, 0.27,
0.33, 0.8, 1, 0, 0.1, 0.31, 0.52, 1, 0, 0.07, 0.27, 0.37, 0.5,
0.77, 1, 0, 0.03, 0.07, 0.23, 0.4, 0.77, 1, 0, 0.13, 0.47, 0.77,
1), .Dim = c(40L, 2L), .Dimnames = list(NULL, c("speedmph", "prob"
)))), .Names = c("lanem", "cars"), row.names = c(NA, 40L), class = "data.frame")
Problem
I created the plot using spdistbc:
cb1 <- ggplot() + geom_point(data = spdistbc, mapping = aes(x=speedmph, y = cprob, color = 'observed')) + facet_wrap(~Lane) + theme_bw() + my.theme()
Which gave me this:
But when I combine another plot from the second data frame using following code:
cb2 <- cb1 + geom_point(data = cspdistbv, mapping = aes(x = cars.speedmph, y = cars.prob, color = 'simulated-default')) + facet_wrap(~lanem)
I get the error:
Error in eval(expr, envir, enclos) : object 'cars.speedmph' not found
Question
You can see in the cspdistbv data frame, there is a column named cars.speedmph, then why R can't find it? Please help.
Somehow you've created an invalid data.frame. You've stored a matrix in the second column of cspdistbv; dim(cspdistbv) thinks it only has two columns and this interferes with proper naming and such. I'm not sure how you created it, but you can fix it with
cspdistbv <- cbind.data.frame(lanem=cspdistbv[,1], cspdistbv[,2])
And then
cb1 <- ggplot() + geom_point(data = spdistbc, mapping = aes(x=speedmph,
y = cprob, color = 'observed')) + facet_wrap(~Lane) + theme_bw()
cb2 <- cb1 + geom_point(data = cspdistbv, mapping = aes(x = speedmph,
y = prob, color = 'simulated-default')) + facet_wrap(~lanem)
should work

How can I plot Facets in R using my data?

I have the following data for a US state
d <- structure(list(Month = structure(c(5L, 4L, 9L, 1L, 8L, 7L, 6L,
2L, 12L, 11L, 10L, 3L), .Label = c("01-Abr-14", "01-Ago-14",
"01-Dez-14", "01-Fev-14", "01-Jan-14", "01-Jul-14", "01-Jun-14",
"01-Mai-14", "01-Mar-14", "01-Nov-14", "01-Out-14", "01-Set-14"
), class = "factor"), Ada = c(0.1, 0.14, 0.25, -0.06, -0.15,
0.3, 0.02, -0.01, 0.37, 0.08, 0.15, 0.17), Altus = c(0.06, 0.05,
0.1, -0.17, -0.02, 0.25, -0.02, 0.08, 0, 0.02, 0.02, 0.02), Antlers = c(-0.08,
0.02, 0.1, -0.38, -0.3, 0.27, -0.17, -0.1, 0.11, 0.11, 0.14,
0), Ardmore = c(-0.01, 0.09, 0.18, -0.24, 0.02, 0.21, -0.13,
-0.06, 0.18, 0.01, -0.02, 0.08), Bartlesville = c(-0.02, 0.09,
0.2, 0.16, -0.07, 0.08, 0.01, 0.04, -0.01, 0.11, 0.11, 0.15),
Beaver = c(0.01, -0.02, 0.16, -0.08, -0.04, 0.07, 0.07, 0.07,
-0.12, 0.05, 0, 0.05), Boise.City = c(0.02, -0.05, 0.09,
-0.1, -0.24, 0.05, -0.01, 0.21, -0.01, -0.06, 0, 0.03), Buffalo = c(-0.04,
0, 0.15, -0.05, -0.15, 0.16, -0.11, 0.12, -0.2, 0.03, 0,
0.01), Carnegie = c(0.02, 0.06, 0.15, -0.16, 0.09, 0.24,
-0.03, 0.09, -0.16, 0.06, -0.01, 0), Cherokee = c(0.06, 0.1,
0.26, -0.1, -0.06, 0.2, 0.06, -0.06, -0.06, -0.11, 0, 0.08
), Claremore = c(-0.02, 0.22, 0.18, 0.12, -0.09, 0.11, 0.16,
0.04, 0.46, 0.16, 0.25, 0.17), Durant = c(0.06, 0.05, 0.15,
-0.11, -0.12, 0.35, -0.21, 0, 0.4, 0.04, 0.26, 0.09), Enid = c(0.07,
0.08, 0.34, 0, 0.18, 0.34, 0.11, 0.09, -0.04, 0.17, 0.13,
0.1), Erick = c(0.05, 0.06, 0.14, -0.15, -0.09, 0.07, -0.05,
0.13, 0.01, -0.02, 0.04, 0.02), Geary = c(-0.01, 0.01, 0.15,
-0.19, -0.09, 0.14, -0.18, 0.14, 0.02, 0.05, -0.07, 0), Goodwell = c(0,
-0.05, 0.08, -0.08, -0.01, 0, -0.06, 0.03, -0.12, 0.02, -0.03,
0), Guthrie = c(0.06, 0.13, 0.23, -0.09, 0.06, 0.31, -0.03,
0.05, -0.01, 0.03, 0.09, 0.11), Hammon = c(0.02, 0.03, 0.14,
-0.2, -0.04, 0.1, -0.1, 0.23, -0.07, 0.05, 0.03, 0.03), Hennessey = c(0.02,
0.09, 0.22, -0.04, 0.1, 0.22, 0.13, 0.2, 0.08, -0.01, 0.03,
0.07), Hobartmuni = c(-0.03, -0.02, 0.1, -0.17, -0.29, -0.08,
0.02, 0.04, -0.05, -0.03, 0, 0), Holdenville = c(-0.04, 0.12,
0.26, 0.05, -0.16, 0.23, 0.04, 0.01, 0.27, 0.13, 0.12, 0.07
), Hooker = c(0.03, -0.03, 0.07, -0.08, -0.17, 0.01, -0.03,
-0.05, -0.14, -0.02, 0, 0.04), Jefferson = c(0.04, 0.05,
0.29, 0.09, 0.05, 0.21, 0.11, 0.07, -0.03, 0.05, 0.09, 0.08
), Kenton = c(0.02, -0.06, 0.05, -0.12, -0.15, 0, 0.27, 0.17,
-0.01, -0.04, -0.02, -0.01), Kingfisher = c(0.05, 0.09, 0.18,
-0.02, 0.19, 0.21, -0.03, 0.19, 0.1, -0.01, 0.02, 0.11),
Lawton = c(0.03, 0.06, 0.06, -0.17, -0.39, 0.11, -0.1, 0.06,
0, 0.06, 0.03, 0.03), Mangum = c(0.01, 0, 0.05, -0.31, -0.27,
0.13, 0.01, -0.01, -0.01, 0, 0, 0.01), Meeker = c(-0.03,
0.14, 0.22, -0.15, -0.03, 0.34, 0.05, -0.03, 0.22, 0.14,
0.02, 0.06), Miami = c(-0.03, 0.03, 0.17, 0.12, 0.15, -0.11,
-0.15, -0.29, 0.34, 0.11, 0.25, 0.09), Muskogee = c(0.08,
0.12, 0.14, -0.04, 0.27, 0.16, -0.09, -0.07, 0.36, -0.02,
0.23, 0.14), Mutual = c(0.04, 0.05, 0.16, -0.05, 0.2, 0.16,
0.06, -0.04, -0.1, 0.02, 0, 0.08), Newkirk = c(-0.04, 0.06,
0.19, 0.13, -0.09, 0.15, 0.24, -0.01, -0.1, 0.21, 0, 0.1),
Okeene = c(0.09, 0.19, 0.12, 0.06, 0.02, 0.41, 0.03, 0.03,
0.37, 0.08, 0.13, 0.17), Okemah = c(0, 0.04, 0.2, -0.08,
0.04, 0.12, 0.05, 0.04, -0.01, 0.1, 0, 0.06), Okmulgee = c(0.04,
0.21, 0.17, -0.02, 0.01, 0.23, 0.03, 0.06, 0.17, 0, 0.29,
0.1), Pauls_valley = c(0.17, 0.17, 0.36, -0.02, -0.11, 0.27,
-0.12, 0.06, 0.27, 0.22, 0.13, 0.21), Pawhuska = c(0.05,
0.11, 0.3, 0.29, 0.19, 0.13, 0.2, -0.02, 0.1, 0.12, 0.2,
0.15), Perry = c(0.04, 0.13, 0.25, -0.15, -0.06, 0.02, 0.2,
-0.15, -0.05, 0, -0.04, 0.07), Poteau = c(-0.03, 0.05, 0.21,
-0.11, -0.01, -0.07, -0.11, -0.15, 0.37, 0.39, 0.25, 0.2),
Stillwater = c(0.04, 0.12, 0.16, -0.2, -0.04, 0.2, 0.1, -0.01,
0.02, 0, 0, 0.05), Tahlequah = c(0.09, 0.17, 0.27, -0.02,
0.48, 0.06, 0.28, 0.05, 0.38, 0.11, 0.4, 0.21), Waurika = c(0.07,
0.04, 0.16, -0.09, -0.12, 0.08, -0.22, -0.01, 0.09, 0.09,
-0.03, 0.02), Weatherford = c(0.03, 0.05, 0.23, -0.24, -0.06,
0.25, -0.11, 0.3, -0.1, 0.04, -0.03, 0.01), Webbersfalls = c(-0.04,
0.1, 0.16, -0.09, 0.09, -0.01, -0.25, -0.18, 0.39, 0.01,
0.3, 0.11)), .Names = c("Month", "Ada", "Altus", "Antlers",
"Ardmore", "Bartlesville", "Beaver", "Boise.City", "Buffalo",
"Carnegie", "Cherokee", "Claremore", "Durant", "Enid", "Erick",
"Geary", "Goodwell", "Guthrie", "Hammon", "Hennessey", "Hobartmuni",
"Holdenville", "Hooker", "Jefferson", "Kenton", "Kingfisher",
"Lawton", "Mangum", "Meeker", "Miami", "Muskogee", "Mutual",
"Newkirk", "Okeene", "Okemah", "Okmulgee", "Pauls_valley", "Pawhuska",
"Perry", "Poteau", "Stillwater", "Tahlequah", "Waurika", "Weatherford",
"Webbersfalls"), class = "data.frame", row.names = c(NA, -12L
))
coordinates for each place to map
coords <- structure(list(place = structure(c(2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 1L, 44L), .Label = c(" Weatherford",
"Ada", "Altus", "Antlers", "Ardmore", "Bartlesville", "Beaver",
"Boise City", "Buffalo", "Carnegie", "Cherokee", "Claremore",
"Durant", "Enid", "Erick", "Geary", "Goodwell", "Guthrie", "Hammon",
"Hennessey", "Hobartmuni", "Holdenville", "Hooker", "Jefferson",
"Kenton", "Kingfisher", "Lawton", "Mangum", "Meeker", "Miami",
"Muskogee", "Mutual", "Newkirk", "Okeene", "Okemah", "Okmulgee",
"Pauls_valley", "Pawhuska", "Perry", "Poteau", "Stillwater",
"Tahlequah", "Waurika", "Webbersfalls"), class = "factor"), Lat = c(34.7864,
34.5903, 34.2208, 34.1714, 36.7683, 36.8125, 36.7236, 36.8003,
35.1756, 36.7747, 36.3225, 34.0003, 36.4194, 35.2164, 35.6267,
36.5914, 35.8161, 35.585, 36.0942, 34.9894, 35.0567, 36.8589,
36.7222, 36.9031, 35.8583, 34.6097, 34.8911, 35.505, 36.8833,
35.7781, 36.2283, 36.8914, 36.1217, 35.4253, 35.6239, 34.7253,
36.6692, 36.2886, 35.0539, 36.1175, 35.9369, 34.1747, 35.52,
35.4814), Long = c(-96.685, -99.3344, -95.615, -97.1294, -96.0261,
-100.5308, -102.4806, -99.6403, -98.5794, -98.3583, -95.5808,
-96.3686, -97.8747, -99.8628, -98.3225, -101.6181, -97.395, -99.3953,
-97.835, -99.0525, -96.3861, -101.2172, -97.7903, -102.965, -97.9294,
-98.4572, -99.5017, -96.9767, -94.8833, -95.3339, -99.17, -97.0586,
-98.315, -96.3033, -96.025, -97.2814, -96.3472, -97.2897, -94.6264,
-97.095, -94.9644, -97.9964, -98.6986, -95.2039)), .Names = c("place",
"Lat", "Long"), class = "data.frame", row.names = c(NA, -44L))
So basically I want to take the values, find out which one is positive and which one is negative and plot a figure like below.
There will be three different tickers for +,- and zero
12 facets for 12 months
I have the lat/longs for the 44 places, so I can plot them on the state map.
How can I do this using ggplot2? or some other utility.
Till now, I have plotted the points using the following code
ggplot() +
geom_polygon(data=m, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
geom_point(data=stations,aes(x=long,y=lat),,colour="red",)+
size=0.5,hjust=0,alpha=.5),size=3)+
xlab('Longitude')+
ylab('Latitude')+
coord_fixed()
Try this
Load lackages, download when necessary
kpacks <- c('raster', 'ggplot2', 'reshape2', 'lubridate')
new.packs <- kpacks[!(kpacks %in% installed.packages()[,"Package"])]
if(length(new.packs)) install.packages(new.packs)
lapply(kpacks, require, character.only=T)
remove(kpacks, new.packs)
d <- read.table(text=readClipboard(), sep = '\t', header=TRUE)
# or copy and paste the data from above
coords <- read.table(text=readClipboard(), sep = '\t', header=TRUE, quote = '')
# or copy and paste the data from above
#coordinates(coords) <- ~Long+Lat #not necessary for this approach
Get data from GADM
usa <- raster::getData(country = 'USA', level = 1)
okl <- usa[usa#data$NAME_1 == 'Oklahoma', ]
#plot(okl)
#plot(coords, add = T, cols = d)
d1 <- melt(d) # reshape it
d1$Month <- lubridate::dmy(d1$Month) # I've used abrev names in Portuguese. Change accordingly
create a factor variable with levels = positive/zero/negative
d1$val <- cut(d1$value, breaks= c(min(d1$value), 0.00, 0.001, max(d1$value)),
labels = c('negative', 'zero', 'positive'),
right = F, include.lowest = T, dig.lab = 3)
d2 <- merge(d1, coords, by.x = 'variable', by.y = 'place', all.x = T)
head(d2)
variable Month value val Lat Long
1 Ada Jul 0.02 positive 34.7864 -96.685
2 Ada May -0.15 negative 34.7864 -96.685
3 Ada Jun 0.30 positive 34.7864 -96.685
4 Ada Jan 0.10 positive 34.7864 -96.685
5 Ada Feb 0.14 positive 34.7864 -96.685
6 Ada Mar 0.25 positive 34.7864 -96.685
okl_df <- fortify(okl) # spdf to data.frame
head(okl_df)
long lat order hole piece group id
1 -95.52363 37.00093 1 FALSE 1 36.1 36
2 -95.40672 37.00047 2 FALSE 1 36.1 36
3 -95.40027 37.00053 3 FALSE 1 36.1 36
4 -95.07227 36.99872 4 FALSE 1 36.1 36
5 -95.03362 36.99859 5 FALSE 1 36.1 36
6 -95.03309 36.99920 6 FALSE 1 36.1 36
p <- ggplot(data = okl_df, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = NA, colour = 'black') +
geom_point(inherit.aes = F, data = d2, aes(x=Long, y = Lat, colour = val)) +
facet_wrap(~ Month, ncol = 3) +
theme_minimal() +
coord_map()+
scale_colour_manual('class', values = c('negative'= 'grey80', 'zero' = 'grey60',
'positive' = 'black'))
p
EDIT
For mapping the variable to shapes instead of colours one can map it to aesthetics
p1 <- ggplot(data = okl_df, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = NA, colour = 'black') +
geom_point(inherit.aes = F, data = d2, aes(x=Long, y = Lat, shape = val)) +
facet_wrap(~ Month, ncol = 3) +
theme_minimal() +
coord_map()
p1
Or map both colour and shape to aesthetics
p2 <- ggplot(data = okl_df, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = NA, colour = 'black') +
geom_point(inherit.aes = F, data = d2, aes(x=Long, y = Lat, colour = val,
shape = val)) +
facet_wrap(~ Month, ncol = 3) +
theme_minimal() +
coord_map()
p2
You can export it as png, manipulating resolution was needed
ggsave(file = file.path(tempdir(),'map1.png'),
p
, width=16, height=16, units = "cm", dpi = 150
)
Add facet_wrap(~ month) or facet_grid(month ~ .) at the end of your plotting code. You can also specify the number of columns in facet_wrap: facet_wrap(~ month, ncol = 3)
I've rearranged your data:
transposed the data
named the first column place
The resulting dataframe:
oklahoma <- structure(list(place = structure(1:44, .Label = c("Ada", "Altus", "Antlers", "Ardmore", "Bartlesville", "Beaver", "Boise City", "Buffalo", "Carnegie", "Cherokee", "Claremore", "Durant", "Enid", "Erick", "Geary", "Goodwell", "Guthrie", "Hammon", "Hennessey", "Hobartmuni", "Holdenville", "Hooker", "Jefferson", "Kenton", "Kingfisher", "Lawton", "Mangum", "Meeker", "Miami", "Muskogee", "Mutual", "Newkirk", "Okeene", "Okemah", "Okmulgee", "Pauls valley", "Pawhuska", "Perry", "Poteau", "Stillwater", "Tahlequah", "Waurika", "Weatherford", "Webbersfalls"), class = "factor"), Jan = structure(c(16L, 12L, 5L, 1L, 2L, 7L, 8L, 4L, 8L, 12L, 2L, 12L, 16L, 11L, 1L, 6L, 12L, 8L, 8L, 3L, 4L, 9L, 10L, 8L, 11L, 9L, 7L, 3L, 3L, 14L, 10L, 4L, 15L, 6L, 10L, 17L, 11L, 10L, 3L, 10L, 15L, 13L, 9L, 4L), .Label = c("-0,01", "-0,02", "-0,03", "-0,04", "-0,08", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,07", "0,08", "0,09", "0,1", "0,17"), class = "factor"), Feb = structure(c(17L, 10L, 7L, 12L, 12L, 1L, 3L, 5L, 11L, 13L, 21L, 10L, 13L, 11L, 6L, 3L, 16L, 8L, 12L, 1L, 15L, 2L, 10L, 4L, 12L, 11L, 5L, 17L, 8L, 15L, 10L, 11L, 19L, 9L, 20L, 18L, 14L, 16L, 10L, 15L, 18L, 9L, 10L, 13L), .Label = c("-0,02", "-0,03", "-0,05", "-0,06", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,09", "0,1", "0,11", "0,12", "0,13", "0,14", "0,17", "0,19", "0,21", "0,22"), class = "factor"), Mar = structure(c(18L, 6L, 6L, 12L, 14L, 10L, 5L, 9L, 9L, 19L, 12L, 9L, 22L, 8L, 9L, 4L, 17L, 8L, 16L, 6L, 19L, 3L, 21L, 1L, 12L, 2L, 1L, 16L, 11L, 8L, 10L, 13L, 7L, 14L, 11L, 23L, 22L, 18L, 15L, 10L, 20L, 10L, 17L, 10L), .Label = c("0,05", "0,06", "0,07", "0,08", "0,09", "0,1", "0,12", "0,14", "0,15", "0,16", "0,17", "0,18", "0,19", "0,2", "0,21", "0,22", "0,23", "0,25", "0,26", "0,27", "0,29", "0,3", "0,36"), class = "factor"), Apr = structure(c(4L, 14L, 17L, 15L, 24L, 5L, 7L, 3L, 11L, 7L, 22L, 8L, 18L, 14L, 13L, 5L, 6L, 14L, 2L, 12L, 19L, 5L, 21L, 9L, 1L, 12L, 16L, 10L, 22L, 2L, 3L, 23L, 20L, 5L, 1L, 1L, 25L, 14L, 8L, 14L, 1L, 6L, 15L, 6L), .Label = c("-0,02", "-0,04", "-0,05", "-0,06", "-0,08", "-0,09", "-0,1", "-0,11", "-0,12", "-0,15", "-0,16", "-0,17", "-0,19", "-0,2", "-0,24", "-0,31", "-0,38", "0", "0,05", "0,06", "0,09", "0,12", "0,13", "0,16", "0,29"), class = "factor"), May = structure(c(10L, 18L, 16L, 20L, 5L, 3L, 13L, 10L, 24L, 4L, 6L, 9L, 28L, 7L, 6L, 1L, 23L, 3L, 25L, 15L, 11L, 12L, 22L, 10L, 27L, 17L, 14L, 2L, 26L, 29L, 28L, 6L, 20L, 21L, 19L, 8L, 27L, 7L, 1L, 3L, 30L, 9L, 4L, 24L), .Label = c("-0,01", "-0,03", "-0,04", "-0,06", "-0,07", "-0,09", "-0,1", "-0,11", "-0,12", "-0,15", "-0,16", "-0,17", "-0,24", "-0,27", "-0,29", "-0,3", "-0,39", "0", "0,01", "0,02", "0,04", "0,05", "0,06", "0,09", "0,1", "0,15", "0,19", "0,2", "0,27", "0,48"), class = "factor"), Jun = structure(c(26L, 24L, 25L, 20L, 11L, 10L, 8L, 18L, 23L, 19L, 13L, 29L, 26L, 10L, 16L, 5L, 27L, 12L, 21L, 3L, 22L, 6L, 20L, 5L, 20L, 13L, 15L, 28L, 4L, 18L, 18L, 17L, 30L, 14L, 22L, 25L, 15L, 7L, 2L, 19L, 9L, 11L, 24L, 1L), .Label = c("-0,01", "-0,07", "-0,08", "-0,11", "0", "0,01", "0,02", "0,05", "0,06", "0,07", "0,08", "0,1", "0,11", "0,12", "0,13", "0,14", "0,15", "0,16", "0,2", "0,21", "0,22", "0,23", "0,24", "0,25", "0,27", "0,3", "0,31", "0,34", "0,35", "0,41"), class = "factor"), Jul = structure(c(17L, 15L, 10L, 8L, 16L, 22L, 1L, 6L, 2L, 21L, 26L, 12L, 23L, 5L, 11L, 3L, 2L, 5L, 25L, 17L, 19L, 2L, 24L, 29L, 2L, 5L, 16L, 20L, 9L, 4L, 21L, 28L, 18L, 20L, 18L, 7L, 27L, 27L, 6L, 23L, 30L, 13L, 6L, 14L), .Label = c("-0,01", "-0,03", "-0,06", "-0,09", "-0,1", "-0,11", "-0,12", "-0,13", "-0,15", "-0,17", "-0,18", "-0,21", "-0,22", "-0,25", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,07", "0,1", "0,11", "0,13", "0,16", "0,2", "0,24", "0,27", "0,28"), class = "factor"), Aug = structure(c(1L, 20L, 8L, 6L, 16L, 19L, 29L, 23L, 21L, 6L, 16L, 13L, 22L, 24L, 25L, 15L, 17L, 30L, 28L, 16L, 14L, 5L, 19L, 26L, 27L, 18L, 1L, 3L, 12L, 7L, 4L, 1L, 15L, 16L, 18L, 18L, 2L, 11L, 9L, 1L, 17L, 1L, 31L, 10L), .Label = c("-0,01", "-0,02", "-0,03", "-0,04", "-0,05", "-0,06", "-0,07", "-0,1", "-0,15", "-0,18", "-0,2", "-0,29", "0", "0,01", "0,03", "0,04", "0,05", "0,06", "0,07", "0,08", "0,09", "0,1", "0,12", "0,13", "0,14", "0,17", "0,19", "0,2", "0,21", "0,23", "0,3"), class = "factor"), Sep = structure(c(24L, 11L, 17L, 19L, 1L, 7L, 1L, 10L, 9L, 4L, 28L, 27L, 11L, 12L, 13L, 7L, 1L, 5L, 14L, 3L, 21L, 8L, 2L, 1L, 16L, 11L, 1L, 20L, 22L, 23L, 6L, 6L, 24L, 1L, 18L, 21L, 16L, 6L, 24L, 13L, 25L, 15L, 6L, 26L), .Label = c("-0,01", "-0,03", "-0,05", "-0,06", "-0,07", "-0,1", "-0,12", "-0,14", "-0,16", "-0,2", "0", "0,01", "0,02", "0,08", "0,09", "0,1", "0,11", "0,17", "0,18", "0,22", "0,27", "0,34", "0,36", "0,37", "0,38", "0,39", "0,4", "0,46"), class = "factor"), Oct = structure(c(14L, 9L, 17L, 8L, 17L, 12L, 5L, 10L, 13L, 6L, 21L, 11L, 22L, 7L, 12L, 9L, 10L, 12L, 1L, 3L, 19L, 2L, 12L, 4L, 1L, 13L, 7L, 20L, 17L, 2L, 9L, 23L, 14L, 16L, 7L, 24L, 18L, 7L, 25L, 7L, 17L, 15L, 11L, 8L), .Label = c("-0,01", "-0,02", "-0,03", "-0,04", "-0,06", "-0,11", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,08", "0,09", "0,1", "0,11", "0,12", "0,13", "0,14", "0,16", "0,2", "0,21", "0,22", "0,39"), class = "factor"), Nov = structure(c(15L, 6L, 14L, 2L, 11L, 5L, 5L, 5L, 1L, 5L, 18L, 19L, 10L, 8L, 4L, 3L, 9L, 7L, 7L, 5L, 12L, 5L, 9L, 2L, 6L, 7L, 5L, 6L, 18L, 17L, 5L, 5L, 13L, 5L, 20L, 13L, 16L, 5L, 18L, 5L, 22L, 3L, 3L, 21L), .Label = c("-0,01", "-0,02", "-0,03", "-0,07", "0", "0,02", "0,03", "0,04", "0,09", "0,1", "0,11", "0,12", "0,13", "0,14", "0,15", "0,2", "0,23", "0,25", "0,26", "0,29", "0,3", "0,4"), class = "factor"), Dec = structure(c(16L, 4L, 2L, 10L, 15L, 7L, 5L, 3L, 2L, 10L, 16L, 11L, 12L, 4L, 2L, 2L, 13L, 5L, 9L, 2L, 9L, 6L, 10L, 1L, 13L, 5L, 3L, 8L, 11L, 14L, 10L, 12L, 16L, 8L, 12L, 18L, 15L, 9L, 17L, 7L, 18L, 4L, 3L, 13L), .Label = c("-0,01", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,07", "0,08", "0,09", "0,1", "0,11", "0,14", "0,15", "0,17", "0,2", "0,21"), class = "factor"), Lat = structure(c(8L, 5L, 4L, 2L, 37L, 40L, 36L, 39L, 13L, 38L, 32L, 1L, 26L, 14L, 21L, 33L, 23L, 19L, 27L, 10L, 12L, 41L, 35L, 44L, 24L, 6L, 9L, 17L, 42L, 22L, 30L, 43L, 29L, 15L, 20L, 7L, 34L, 31L, 11L, 28L, 25L, 3L, 18L, 16L), .Label = c("34", "34,1714", "34,1747", "34,221", "34,6", "34,61", "34,7253", "34,786", "34,8911", "34,9894", "35,054", "35,0567", "35,1756", "35,2", "35,4253", "35,4814", "35,505", "35,52", "35,585", "35,6239", "35,63", "35,7781", "35,816", "35,8583", "35,9369", "36", "36,0942", "36,1175", "36,122", "36,228", "36,3", "36,3225", "36,5914", "36,6692", "36,7222", "36,7236", "36,7683", "36,7747", "36,8", "36,813", "36,859", "36,88", "36,8914", "36,903"), class = "factor"), Long = structure(c(20L, 39L, 13L, 25L, 15L, 2L, 5L, 44L, 37L, 35L, 12L, 18L, 32L, 1L, 33L, 4L, 27L, 42L, 29L, 40L, 19L, 3L, 28L, 6L, 30L, 36L, 43L, 21L, 8L, 11L, 41L, 23L, 34L, 16L, 14L, 26L, 17L, 22L, 7L, 24L, 9L, 31L, 38L, 10L), .Label = c("-100", "-100,5", "-101,2", "-101,618", "-102,481", "-103", "-94,63", "-94,9", "-94,9644", "-95,2039", "-95,3339", "-95,5808", "-95,62", "-96,025", "-96,0261", "-96,303", "-96,3472", "-96,37", "-96,3861", "-96,69", "-96,98", "-97", "-97,059", "-97,095", "-97,129", "-97,2814", "-97,4", "-97,7903", "-97,835", "-97,9294", "-97,996", "-98", "-98,3", "-98,32", "-98,3583", "-98,46", "-98,579", "-98,6986", "-99", "-99,0525", "-99,17", "-99,3953", "-99,502", "-99,64"), class = "factor")), .Names = c("place", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Lat", "Long"), class = "data.frame", row.names = c(NA, -44L))
After that reshape the dataframe:
require(reshape2)
stations <- melt(oklahoma, id=c("place","Lat","Long"))
stations$value <- gsub(',', '.', stations$value)
stations$value <- as.numeric(stations$value)
The create the plot:
ggplot() +
# geom_polygon(data=m, aes(x=Long, y=Lat,group=group), colour="black", fill="white") +
geom_point(data=stations, aes(x=Long,y=Lat), colour="red")+
xlab('Longitude') +
ylab('Latitude') +
coord_fixed() +
facet_wrap(~ variable, ncol = 3)
As you can see, I commented out the geom_polygon as I don't have that data.

Resources