Using date time data with ggplot scale_colour_gradient - r
I am plotting some time series GPS coordinates using ggmap and ggplot. I want to visualise the time series by creating a colour gradient. I have had a couple of attempts so far as shown below.
My data can be accessed here
Import data
Dec7 = read.csv("7-12-15.csv", header = TRUE, stringsAsFactors = FALSE)
Dec7$timestamp <- as.Date(Dec7$timestamp)
head(Dec7)
X_id seq_id timestamp lon address lat rssi sensor gps_quality batt_v
1 56656ecd0dd8e408d8c2e43f 71 2015-12-07 -3.780899 208 53.20252 -63 gps 1 3274
2 56656ed20dd8e408d8c2e440 72 2015-12-07 -3.780958 208 53.20246 -63 gps 1 3274
3 56656edc0dd8e408d8c2e441 73 2015-12-07 -3.780967 208 53.20246 -65 gps 1 3274
4 56656ee60dd8e408d8c2e442 74 2015-12-07 -3.780968 208 53.20242 -64 gps 1 3274
5 56656ef10dd8e408d8c2e443 75 2015-12-07 -3.780997 208 53.20240 -64 gps 1 3274
6 56656efa0dd8e408d8c2e446 76 2015-12-07 -3.780965 208 53.20243 -64 gps 1 3274
str(Dec7)
data.frame': 22420 obs. of 10 variables:
$ X_id : chr "56656ecd0dd8e408d8c2e43f" "56656ed20dd8e408d8c2e440" "56656edc0dd8e408d8c2e441" "56656ee60dd8e408d8c2e442" ...
$ seq_id : int 71 72 73 74 75 76 77 78 86 87 ...
$ timestamp : Date, format: "2015-12-07" "2015-12-07" "2015-12-07" "2015-12-07" ...
$ lon : num -3.78 -3.78 -3.78 -3.78 -3.78 ...
$ address : num 208 208 208 208 208 208 208 208 208 208 ...
$ lat : num 53.2 53.2 53.2 53.2 53.2 ...
$ rssi : int -63 -63 -65 -64 -64 -64 -64 -63 -64 -64 ...
$ sensor : chr "gps" "gps" "gps" "gps" ...
$ gps_quality: int 1 1 1 1 1 1 1 1 1 1 ...
$ batt_v : int 3274 3274 3274 3274 3274 3274 3274 3274 3274 3274 ...
I have classed timestamp as.Date as I am aware that this can then be passed successfully into scale_colour_gradient within the ggplot call as follows:
mapImageData <- get_googlemap(center = c(lon = median(Dec7$lon),
lat = median(Dec7$lat)), zoom = 17,
size = c(500, 500),
maptype = c("satellite"))
sheep_hiraetlyn_Dec7_map = ggmap(mapImageData,extent = "device") +
geom_point(aes(x = lon,y = lat, color=timestamp),
data = Dec7, size = 1, pch = 20) +
scale_color_gradient(trans = "date", low="red", high="blue")
This produces the following map:
As you can see the colour gradient is discrete rather than the desired continuous gradient - presumably this is because it categorizes the timestamp into days?
Also The legend labels consist of 2 overlayed labels so are not clear.
I have tried using as.POSIXct but this cannot be passed to trans.
I have also used as.Integer, which creates a nice colour gradient but the legend cannot be interpreted in terms of date/time.
Any ideas how I can get round this problem?
Thanks
Convert the POSIXct timestamps to integer and define the breaks and labels manually
Dec7$time <- as.integer(Dec7$timestamp)
labels <- pretty(Dec7$timestamp, 5)
ggmap(mapImageData,extent = "device") +
geom_point(
aes(x = lon,y = lat, color=time),
data = Dec7, size = 1, pch = 20
) +
scale_color_gradient(
low="red", high="blue",
breaks = as.integer(labels),
labels = format(labels, "%m/%d %H:%M")
)
Related
Create a dataframe i nR
I would like to create a dataframe with 117 columns and 90 rows, the first ones being: ID, date1, date2, Category, DR1, DRM01, DRM02, DRM03 .... up to DRM111. For the first column, it would have values ranging from 1 to 3. In date1 it would have a fixed value, which would be "2022-01-05", in date2, it would have values between 2021-12-20 to the maximum that it gives. Category can be ABC or ERF, in DR1 would be values that would vary from 200 to 250, and finally, in DRM columns, would be values that would vary from 0 to 300. Is it possible to create a dataframe like this?
I wondering if this is an effort at simulation. The first few tasks seem blindly obvious but the last call to replicate with simplify=FALSE might have been a bit less than trivial. test <- data.frame( ID = rep(1:3, length=90), date1 = as.Date( "2022-01-05"), date2= seq( as.Date("2021-12-20"), length.out=90, by=1), #Category = ???? so far not specified DR1 = sample( 200:250, 90, repl=TRUE), #need repl is length need is long setNames( replicate(111, { sample(0:300, 90)}, simplify=FALSE) , nm=paste("DRM",1:111) ) ) Snipped the last 105 rows of the output from str: str(test) 'data.frame': 90 obs. of 115 variables: $ ID : int 1 2 3 1 2 3 1 2 3 1 ... $ date1 : Date, format: "2022-01-05" "2022-01-05" "2022-01-05" "2022-01-05" ... $ data2 : Date, format: "2021-12-20" "2021-12-21" "2021-12-22" "2021-12-23" ... $ DR1 : int 229 218 240 243 221 202 242 221 237 208 ... $ DRM.1 : int 41 238 142 100 19 56 224 152 85 84 ... $ DRM.2 : int 150 185 141 55 34 83 88 105 165 294 ... $ DRM.3 : int 144 22 237 174 78 291 120 63 261 236 ... $ DRM.4 : int 223 105 263 214 45 226 129 80 182 15 ... $ DRM.5 : int 27 108 288 237 129 251 150 70 300 243 ... # additional rows elided The last item in that construction returns a list that has 111 "columns" with ascending numbered names. I admit to being puzzled about why there were periods in the DRM names but then realized that the data.frame function uses check.names to make sure they are legitimate, so the spaces from paste were converted to periods. If you don't like periods then use paste0.
ggplot_line: label the top 2 peak with X-axis values
I am new to R programming. I am plotting a mass spectrum with ggplot and would like to label the top 2 peaks with their x-axis values (i.e. m). Does anyone know how to achieve that? Thanks so much for your help! Here is part of the raw data I used for the ggplot. m Intensity 1 30001 2.964e+01 2 30002 3.336e+01 3 30003 3.968e+01 4 30004 5.015e+01 5 30005 6.838e+01 6 30006 1.016e+02 7 30007 1.464e+02 8 30008 2.130e+02 9 30009 3.115e+02 10 30010 3.951e+02 11 30011 5.134e+02 12 30012 5.316e+02 13 30013 6.377e+02 14 30014 8.813e+02 15 30015 1.071e+03 16 30016 1.119e+03 17 30017 1.202e+03 18 30018 1.299e+03 19 30019 1.112e+03 20 30020 1.205e+03 21 30021 1.422e+03 22 30022 1.653e+03 23 30023 1.726e+03 24 30024 2.423e+03 25 30025 3.059e+03 26 30026 3.267e+03 27 30027 3.993e+03 28 30028 5.172e+03 29 30029 5.278e+03 30 30030 2.794e+03 31 30031 1.459e+03 32 30032 2.512e+03 33 30033 6.590e+03 34 30034 1.245e+04 35 30035 1.144e+04 36 30036 5.197e+03 37 30037 6.012e+03 38 30038 1.453e+04 39 30039 1.513e+04 40 30040 5.802e+03 41 30041 9.226e+03 42 30042 5.809e+03 43 30043 3.074e+03 44 30044 3.882e+03 45 30045 9.941e+02 46 30046 8.170e+02 47 30047 1.149e+03 48 30048 3.567e+02 49 30049 3.805e+02 50 30050 3.654e+02 51 30051 4.724e+02 52 30052 7.819e+02 53 30053 8.634e+02 54 30054 5.235e+02 55 30055 1.712e+02 56 30056 9.232e+01 57 30057 9.434e+01 58 30058 7.191e+01 59 30059 8.036e+01 60 30060 4.456e+01 61 30061 9.428e+01 62 30062 9.392e+01 63 30063 8.413e+01 64 30064 5.671e+01 65 30065 2.639e+01 66 30066 2.027e+01 67 30067 4.584e+01 68 30068 6.956e+01 69 30069 6.181e+01 70 30070 6.450e+01 71 30071 2.826e+01 72 30072 3.610e+01 73 30073 6.325e+01 74 30074 3.509e+01 75 30075 3.478e+01 76 30076 1.120e+01 77 30077 6.993e+00 78 30078 9.936e+00 79 30079 7.738e+00 80 30080 9.771e+00 81 30081 1.762e+01 82 30082 3.060e+01 83 30083 2.175e+01 84 30084 2.816e+01 85 30085 2.700e+01 86 30086 2.114e+01 87 30087 4.378e+01 88 30088 5.824e+01 89 30089 6.193e+01 90 30090 4.146e+01 91 30091 9.697e+04 92 30092 9.458e+04 93 30093 9.216e+04 94 30094 8.972e+04 95 30095 8.723e+04 96 30096 8.468e+04 97 30097 8.211e+04 98 30098 7.959e+04 99 30099 7.726e+04 100 30100 7.527e+04 101 30101 7.379e+04 102 30102 7.298e+04 103 30103 7.301e+04 104 30104 7.399e+04 105 30105 7.602e+04 106 30106 7.916e+04 107 30107 8.340e+04 108 30108 8.862e+04 109 30109 9.460e+04 110 30110 1.010e+05 111 30111 1.074e+05 112 30112 1.133e+05 113 30113 1.180e+05 114 30114 1.211e+05 115 30115 1.222e+05 116 30116 1.213e+05 117 30117 1.186e+05 118 30118 1.146e+05 119 30119 1.100e+05 120 30120 1.054e+05 121 30121 1.014e+05 122 30122 9.838e+04 123 30123 9.637e+04 124 30124 9.535e+04 125 30125 9.508e+04 126 30126 9.520e+04 127 30127 9.527e+04 128 30128 9.484e+04 129 30129 9.355e+04 130 30130 9.128e+04 131 30131 8.809e+04 132 30132 8.425e+04 133 30133 8.012e+04 134 30134 7.603e+04 135 30135 7.225e+04 136 30136 6.895e+04 137 30137 6.617e+04 138 30138 6.392e+04 139 30139 6.214e+04 140 30140 6.078e+04 141 30141 5.980e+04 142 30142 5.922e+04 143 30143 5.905e+04 144 30144 5.934e+04 145 30145 6.013e+04 146 30146 6.143e+04 147 30147 6.324e+04 148 30148 6.552e+04 149 30149 6.816e+04 150 30150 7.100e+04 151 30151 7.384e+04 152 30152 7.655e+04 153 30153 7.904e+04 154 30154 8.132e+04 155 30155 8.353e+04 156 30156 8.595e+04 157 30157 8.896e+04 158 30158 9.302e+04 159 30159 9.864e+04 160 30160 1.063e+05 161 30161 1.165e+05 162 30162 1.293e+05 163 30163 1.443e+05 164 30164 1.605e+05 165 30165 1.759e+05 166 30166 1.883e+05 167 30167 1.957e+05 168 30168 1.969e+05 169 30169 1.921e+05 170 30170 1.824e+05 171 30171 1.693e+05 172 30172 1.544e+05 173 30173 1.390e+05 174 30174 1.241e+05 175 30175 1.102e+05 176 30176 9.755e+04 177 30177 8.644e+04 178 30178 7.692e+04 179 30179 6.900e+04 180 30180 6.262e+04 181 30181 5.766e+04 182 30182 5.397e+04 183 30183 5.137e+04 184 30184 4.972e+04 185 30185 4.889e+04 186 30186 4.881e+04 187 30187 4.940e+04 188 30188 5.059e+04 189 30189 5.230e+04 190 30190 5.444e+04 191 30191 5.690e+04 192 30192 5.960e+04 193 30193 6.244e+04 194 30194 6.539e+04 195 30195 6.842e+04 196 30196 7.153e+04 197 30197 7.471e+04 198 30198 7.795e+04 199 30199 8.118e+04 200 30200 8.430e+04 201 30201 8.719e+04 202 30202 8.976e+04 203 30203 9.193e+04 204 30204 9.364e+04 205 30205 9.480e+04 206 30206 9.531e+04 207 30207 9.504e+04 208 30208 9.391e+04 209 30209 9.189e+04 210 30210 8.912e+04 211 30211 8.587e+04 212 30212 8.251e+04 213 30213 7.939e+04 214 30214 7.680e+04 215 30215 7.492e+04 216 30216 7.381e+04 217 30217 7.349e+04 218 30218 7.394e+04 219 30219 7.510e+04 220 30220 7.690e+04 221 30221 7.919e+04 222 30222 8.174e+04 223 30223 8.425e+04 224 30224 8.637e+04 225 30225 8.776e+04 226 30226 8.826e+04 227 30227 8.788e+04 228 30228 8.690e+04 229 30229 8.569e+04 230 30230 8.465e+04 231 30231 8.405e+04 232 30232 8.398e+04 233 30233 8.434e+04 234 30234 8.494e+04 235 30235 8.554e+04 236 30236 8.598e+04 237 30237 8.623e+04 238 30238 8.638e+04 239 30239 8.665e+04 240 30240 8.736e+04 241 30241 8.884e+04 242 30242 9.147e+04 243 30243 9.559e+04 244 30244 1.016e+05 245 30245 1.097e+05 246 30246 1.200e+05 247 30247 1.321e+05 Here is my code for ggplot: ggplot(data=raw.1) + geom_line(mapping = aes(x=m, y=Intensity)) Below is the ggplot output:
I would do it this way. My solution requires the ggrepel package as well as some dplyr functions. The key to this working is that you can set data = for each geom_ layer in ggplot2. The geom_text_repel() layer from ggrepel ensures that the labels will not overlap your data from geom_line(). library(ggplot2) library(dplyr) library(ggrepel) ggplot(mapping = aes(x = m, y = Intensity, label = m)) + geom_line(data=raw.1) + geom_text_repel(data = raw.1 %>% arrange(desc(Intensity)) %>% # arranges in descending order slice_head(n = 2)) # only keeps the top two intensities. My plot does not look like yours since you only shared the first 247 data points. I suspect that this initial solution might not work for you because I am a chemist and have some idea what you hope to accomplish. This approach labels the top two highest intensities, not necessarily the top two peaks. We need to identify local all maxima and then select the two tallest. Here is how we do that. The following code calculates the slope between each point, and then looks for points where a positive slope changes to a negative slope (local maximum), then it sorts and selects the top two by intensity. top_two <- raw.1 %>% mutate(deriv = Intensity - lag(Intensity) , max = case_when(deriv >=0 & lead(deriv) <0 ~ T, T ~ F)) %>% filter(max) %>% arrange(desc(Intensity)) %>% slice_head(n = 2) Let's modify the original plot code to put this in. ggplot(mapping = aes(x = m, y = Intensity, label = m)) + geom_line(data = raw.1) + geom_text_repel(data = top_two, nudge_y = 1e4) Data: raw.1 <- structure(list(m = c(30001, 30002, 30003, 30004, 30005, 30006, 30007, 30008, 30009, 30010, 30011, 30012, 30013, 30014, 30015, 30016, 30017, 30018, 30019, 30020, 30021, 30022, 30023, 30024, 30025, 30026, 30027, 30028, 30029, 30030, 30031, 30032, 30033, 30034, 30035, 30036, 30037, 30038, 30039, 30040, 30041, 30042, 30043, 30044, 30045, 30046, 30047, 30048, 30049, 30050, 30051, 30052, 30053, 30054, 30055, 30056, 30057, 30058, 30059, 30060, 30061, 30062, 30063, 30064, 30065, 30066, 30067, 30068, 30069, 30070, 30071, 30072, 30073, 30074, 30075, 30076, 30077, 30078, 30079, 30080, 30081, 30082, 30083, 30084, 30085, 30086, 30087, 30088, 30089, 30090, 30091, 30092, 30093, 30094, 30095, 30096, 30097, 30098, 30099, 30100, 30101, 30102, 30103, 30104, 30105, 30106, 30107, 30108, 30109, 30110, 30111, 30112, 30113, 30114, 30115, 30116, 30117, 30118, 30119, 30120, 30121, 30122, 30123, 30124, 30125, 30126, 30127, 30128, 30129, 30130, 30131, 30132, 30133, 30134, 30135, 30136, 30137, 30138, 30139, 30140, 30141, 30142, 30143, 30144, 30145, 30146, 30147, 30148, 30149, 30150, 30151, 30152, 30153, 30154, 30155, 30156, 30157, 30158, 30159, 30160, 30161, 30162, 30163, 30164, 30165, 30166, 30167, 30168, 30169, 30170, 30171, 30172, 30173, 30174, 30175, 30176, 30177, 30178, 30179, 30180, 30181, 30182, 30183, 30184, 30185, 30186, 30187, 30188, 30189, 30190, 30191, 30192, 30193, 30194, 30195, 30196, 30197, 30198, 30199, 30200, 30201, 30202, 30203, 30204, 30205, 30206, 30207, 30208, 30209, 30210, 30211, 30212, 30213, 30214, 30215, 30216, 30217, 30218, 30219, 30220, 30221, 30222, 30223, 30224, 30225, 30226, 30227, 30228, 30229, 30230, 30231, 30232, 30233, 30234, 30235, 30236, 30237, 30238, 30239, 30240, 30241, 30242, 30243, 30244, 30245, 30246, 30247), Intensity = c(29.64, 33.36, 39.68, 50.15, 68.38, 101.6, 146.4, 213, 311.5, 395.1, 513.4, 531.6, 637.7, 881.3, 1071, 1119, 1202, 1299, 1112, 1205, 1422, 1653, 1726, 2423, 3059, 3267, 3993, 5172, 5278, 2794, 1459, 2512, 6590, 12450, 11440, 5197, 6012, 14530, 15130, 5802, 9226, 5809, 3074, 3882, 994.1, 817, 1149, 356.7, 380.5, 365.4, 472.4, 781.9, 863.4, 523.5, 171.2, 92.32, 94.34, 71.91, 80.36, 44.56, 94.28, 93.92, 84.13, 56.71, 26.39, 20.27, 45.84, 69.56, 61.81, 64.5, 28.26, 36.1, 63.25, 35.09, 34.78, 11.2, 6.993, 9.936, 7.738, 9.771, 17.62, 30.6, 21.75, 28.16, 27, 21.14, 43.78, 58.24, 61.93, 41.46, 96970, 94580, 92160, 89720, 87230, 84680, 82110, 79590, 77260, 75270, 73790, 72980, 73010, 73990, 76020, 79160, 83400, 88620, 94600, 101000, 107400, 113300, 118000, 121100, 122200, 121300, 118600, 114600, 110000, 105400, 101400, 98380, 96370, 95350, 95080, 95200, 95270, 94840, 93550, 91280, 88090, 84250, 80120, 76030, 72250, 68950, 66170, 63920, 62140, 60780, 59800, 59220, 59050, 59340, 60130, 61430, 63240, 65520, 68160, 71000, 73840, 76550, 79040, 81320, 83530, 85950, 88960, 93020, 98640, 106300, 116500, 129300, 144300, 160500, 175900, 188300, 195700, 196900, 192100, 182400, 169300, 154400, 139000, 124100, 110200, 97550, 86440, 76920, 69000, 62620, 57660, 53970, 51370, 49720, 48890, 48810, 49400, 50590, 52300, 54440, 56900, 59600, 62440, 65390, 68420, 71530, 74710, 77950, 81180, 84300, 87190, 89760, 91930, 93640, 94800, 95310, 95040, 93910, 91890, 89120, 85870, 82510, 79390, 76800, 74920, 73810, 73490, 73940, 75100, 76900, 79190, 81740, 84250, 86370, 87760, 88260, 87880, 86900, 85690, 84650, 84050, 83980, 84340, 84940, 85540, 85980, 86230, 86380, 86650, 87360, 88840, 91470, 95590, 101600, 109700, 120000, 132100 )), row.names = c(NA, -247L), class = c("tbl_df", "tbl", "data.frame" ))
This approach assumes or treats your x-axis as discrete values of a continuous variable and finds the local maxima based on 2nd derivative using code from Finding local maxima and minima Rest of the plotting is similar to Ben Norris's answer using geom_text_repel() to label the points of interest. Also as noted, the data your provided are different vs. the figure in your question. library(ggplot2) library(ggrepel) # find local maxima aka peaks local_maximas <- raw.1[which(diff(sign(diff(raw.1$Intensity)))==-2)+1,] top2 <- tail(local_maximas[order(local_maximas$Intensity),],2) #subset of top 2 highest peaks raw.1$label <- ifelse(raw.1$m %in% top2$m, raw.1$m, NA) #make labels for plot ggplot(data = raw.1) + geom_line(aes(x=m, y=Intensity)) + geom_text_repel(aes(x = m, y = Intensity, label = label))
Failure passing plots to `saveHTML(){animation}` in R
The background: I am trying to create an animation with saveHTML(){animation} to show how runner's pace between two consecutive laps changes over time. I tried to pass the plots into the expr block with the following code: MakeSpLaps <- function(finishers.pace, lap1, lap2, start.lap) { sp <- qplot(lap1, lap2, data=finishers.pace, color=gender, alpha = I(.7) ) # + additional elements removed; return(sp) } MakeSpLapsAnimation <- function(){ brk <- seq(0, 3000, 60) lbl <- seconds_to_period(brk) oopt = ani.options(interval = 0.2, nmax = 20) saveHTML({ par(mar = c(4, 4, 0.5, 0.5)) for (i in 3:11){ # The problematic line below MakeSpLaps(p, p[[i]], p[[i+1]], i-2) ani.pause() } }, img.name = "lap_plot", imgdir = "lap_dir", htmlfile = "laps.html", autobrowse = FALSE, title = "Plots of consecutive laps.", description = "Plots of consecutive laps.") } Where the data.frame p looks like this: 'data.frame': 17051 obs. of 11 variables: $ bib : int 10001 10003 10004 10005 10006 10009 10010 10011 10012 10013 ... $ gender : Factor w/ 3 levels "","F","M": 3 3 3 3 3 3 3 3 3 3 ... $ X5km_lap : num 290 204 196 315 228 ... $ X10km_lap : num 280 204 201 322 225 ... $ X15km_lap : num 283 205 204 326 235 ... $ X20km_lap : num 282 206 204 342 229 ... $ X25km_lap : num 280 210 205 371 235 ... $ X30km_lap : num 280 225 216 407 254 ... $ X35km_lap : num 279 274 231 404 267 ... $ X40km_lap : num 284 251 257 357 262 ... $ Finish_lap: num 289 242 247 333 265 ... The problem and question: Running MakeSpLaps(p, p[[3]], p[[4]], 1) alone creates the graph I want, but when I plug it to saveHTML(), no plot was created except for a blank PNG. The HTML files are created with the following warning. How can I correctly pass the plots to the function saveHTML()? animation option 'nmax' changed: 20 --> 1 animation option 'nmax' changed: 1 --> 20 HTML file created at: laps.html The actual code is here: https://github.com/hktang/rscraper/blob/3d542b18b5f6fbf1a1fa31b0bd3936f1179cdc59/r/visuals.R#L145
can't draw the grouped value above stacked bar plot in ggplot2
I have a ggplot2 question, I run the code below show the stacked barplot without add value above each bar correctly: p=ggplot(data=essnn) p+geom_bar(binwidth=0.5,stat="identity")+ # aes(x=reorder(classname,-amount,sum), y=amount, label=amount, fill = sort(year))+ theme() I want add the sum amount grouped by year in each class, and here is my code: +geom_text(aes(x=classes,y=total,label=total), data=essnnta, fill=NULL, size=3) But an error message appear: Error in fill = year, can not find object "year" That's my problem: why the object "year" can be found when I draw stack bar plot without add the sum amount grouped by year in each class, but when I add the sum amount grouped by year, the error appear? > str(essnn) 'data.frame': 48619 obs. of 15 variables: $ id : int 2006051337 2006051337 2006051337 2006051337 2006051337 2006051337 2004070648 2006031360 2006031360 2004070062 ... $ gender : Factor w/ 3 levels "","F","M": 3 3 3 3 3 3 3 3 3 3 ... $ age : num 30 30 30 30 30 30 38 43 43 37 ... $ class : Factor w/ 92 levels "100ab","100aa",..: 18 18 18 18 18 18 18 18 18 18 ... $ classname: Factor w/ 1136 levels "cad"," Office2010",..: 111 111 111 111 111 111 116 107 107 107 ... $ grade : num 7 5 6 8 3 4 1 4 3 2 ... $ year : Factor w/ 6 levels "98","99","100",..: 3 3 3 3 2 2 4 5 5 3 ... $ ses : num 212 210 211 213 207 208 217 221 220 210 ... $ date : int 1010421 1001115 1010214 1010701 1000411 1000627 1020424 1030304 1021121 1001108 ... $ money : num 5800 5800 5800 5800 5200 5200 3000 0 5500 5500 ... $ discount : num 1160 1160 1160 1160 1040 1040 600 0 275 550 ... $ amount : num 4640 4640 4640 4640 4160 ... $ idc : Factor w/ 7 levels "在校生","校友",..: 2 2 2 2 2 2 2 7 7 7 ... $ mdy : Date, format: "2012-04-21" "2011-11-15" "2012-02-14" "2012-07-01" ... $ day : num 1123 1281 1190 1052 1499 ... > str(essnnta) 'data.frame': 10 obs. of 2 variables: $ classes: Factor w/ 10 levels "JD","JF",..: 1 7 8 4 6 10 3 5 2 9 $ total : num 55603526 43708950 43555010 35649129 33214372 ...
Your problem might be that your x-axes are not the same in the two data frames. So ggplot does not know which value corresponds with which stack. I am not sure about this as I don't understand the way you define your x axis in the original barplot. I also find it a bit strange to define the aes outside of the ggplot function or the geom_bar. But that might just be me be used to a different kind of syntax. All in all I find it difficult to help you as you do not provide any reproducible example.
Here is a small bit of data, and a plot that sort of works. If you supplement your question with your data (or a subset of it), see if this works. You may also want to position the label at the top of each bar. essnn <- data.frame(year = c(98,99,100,101,102), classname = c("a", "b", "c", "d", "e"), amount = c(1e6, 2e6,3e6,4e6,5e6)) essnnta <- data.frame(total = c(10, 20, 30, 40, 50)) ggplot(data=essnn, aes(x=reorder(classname,-amount, sum), y=amount, fill = year)) + geom_bar(binwidth=0.5, stat="identity", position = "stack") + geom_text(aes(x=essnn$classname, y=essnnta$total, label=essnnta$total), size=3) # not "classes"
mapping chemical concentrations with ggplot
I am trying to make map to show the concentrations of Chromium recorded in topsoil in Scotland (n = 1000). The following is a sub-set of the data: Easting Northing Concentration 1 -4.327230 55.94000 1.913814 2 -4.336588 55.77886 1.408240 3 -4.334057 55.93637 1.798651 4 -4.340633 55.94451 1.629410 5 -4.341627 55.77247 1.382017 6 -4.354362 55.78004 1.432969 7 -4.327912 55.94871 1.488551 8 -4.301948 55.77286 1.278754 9 -4.317669 55.77715 1.465383 10 -4.266635 55.77981 1.793092 11 -4.349507 55.77358 1.336460 12 -4.331458 55.92509 1.546543 13 -4.360420 55.77211 1.720986 14 -4.316048 55.93779 1.876795 15 -4.348813 55.92620 1.637490 16 -4.358550 55.92574 1.460898 17 -4.271819 55.88522 2.011570 18 -4.350699 55.93884 1.385606 19 -4.323065 55.78208 1.620136 20 -4.305748 55.94769 1.463893 21 -4.324094 55.76453 1.416641 22 -4.311998 55.77294 1.390935 23 -4.295788 55.77657 1.378398 24 -4.351286 55.94323 1.485721 25 -4.344118 55.78473 1.623249 26 -4.358147 55.93492 1.454845 27 -4.310889 55.78653 1.372912 28 -4.270665 55.77506 1.706718 29 -4.341747 55.78244 1.561101 30 -4.312615 55.93929 1.521138 31 -4.330014 55.78626 1.564666 32 -4.328320 55.95283 2.313656 33 -4.334340 55.93043 2.007748 34 -4.317788 55.76303 1.309630 35 -4.342244 55.93936 1.680336 36 -4.351105 55.94818 1.673942 37 -4.351354 55.93379 1.396199 38 -4.318706 55.93135 1.854913 39 -4.315999 55.93428 1.361728 40 -4.326163 55.78588 1.646404 41 -4.302010 55.78203 2.023664 42 -4.318585 55.78720 1.305351 43 -4.304388 55.94097 1.465383 44 -4.309106 55.93414 1.539076 45 -4.297275 55.77474 1.503791 46 -4.298785 55.93290 1.447158 47 -4.326837 55.77311 1.555094 48 -4.342423 55.92641 1.338456 49 -4.332528 55.77228 1.491362 50 -4.347461 55.78197 1.426511 str(dat.tmp) 'data.frame': 50 obs. of 3 variables: $ Easting : num -4.33 -4.34 -4.33 -4.34 -4.34 ... $ Northing : num 55.9 55.8 55.9 55.9 55.8 ... $ Concentration: num 1.91 1.41 1.8 1.63 1.38 ... This is the code I am currently using to produce the concentrations on a map of Glasgow: qmap(location="glasgow", maptype = "terrain",zoom=10,color="bw" ,extent="panel", maprange=FALSE) + stat_contour(data = dat.tmp, geom="polygon", aes(x =Easting, y = Northing, z = Concentration , fill = ..level.. ) ) + scale_fill_continuous(name = "Cu (mg/kg)", low = "yellow", high = "red" ) When executing, this is returning: Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0 In addition: Warning message: Not possible to generate contour data This is a similar issue to a previous post - the map / plot I am trying create is very similar too. Filled contour plot with R/ggplot/ggmap Any help would be greatly appreciated - thank you.
I've reproduced the error, and also spotted a warning: Warning message: Not possible to generate contour data A quick google points at a similar question, which is resolved by using stat_density2d instead of stat_contour: qmap(location="glasgow", maptype = "terrain", zoom=10, color="bw", extent="panel", maprange=FALSE) + stat_density2d(data=dat, aes(x=Easting, y=Northing, z=Concentration, fill=..level.. ))