I am a novice at R coding and am trying to plot GW contours using X (Easting) and Y (Northing) cords and GW level (rswl) data in ggplot. An example of the data that I am trying to plot is:
X Obs_No Season Easting Northing rswl
1 56 ADE146 Winter 2017 275638.7 6131431 5.72
2 113 YAT099 Winter 2017 271723.0 6133405 3.16
4 227 YAT066 Winter 2017 276503.0 6135636 2.31
5 292 YAT053 Winter 2017 277780.8 6139285 -2.30
6 400 YAT129 Winter 2017 282065.1 6146759 5.60
7 509 PTA040 Winter 2017 270868.0 6150199 1.68
An example of the code I have tried is:
ggplot(data)+
aes(x = Easting, y = Northing, z = rswl, fill = rswl)+
geom_tile()+
geom_contour(colour = "white", alpha = 0.5)+
scale_fill_distiller(palette = "Spectral", na.value = "white") +
theme_bw()
but it comes up with "Not possible to generate contour data"
Something else I tried with 1 of my datasets is:
ggplot(data, aes(x = Easting, y = Northing, z = rswl)) +
geom_density_2d(colour = "black")+
geom_point(aes(color = factor(Obs_No)))+
theme(legend.title = element_blank())+
ggtitle("Tomw.T2 Winter 2017.csv")
This seems to be contours based on the distribution of points and has nothing to do with the GW level.
Any tips would be greatly appreciated.
Thanks
Related
I need some help with a graph in R.
This is how my dataframe looks like
Footprint
Local Number
Remote Number
Location
10.4
45
4
L1
12.5
452
78
L9
15.6
86
52
L5
85.3
12
12
L4
12.5
35
36
L2
85.9
78
78
L3
78.5
44
44
L6
4.6
10
11
L7
13.9
157
2
L8
What I want to achieve is a graph with the 'Footprint' column in the y-axis, the 'Local Number' column(in the x-axis) in the positive grid of the graph and the 'Remote Number' column(in the x-axis) in the negative grid of the graph. The data should be presented in dots and the lab name should be the label. So basically, I want to show for each location the remote and local number of employees.
I am struggling on presenting the two columns in the x-axis. I appreciate the help!
Maybe you want something like where you could use geom_point for both columns with one negative and positive and add labels using geom_text like this:
df <- read.table(text = 'Footprint Local_Number Remote_Number Location
10.4 45 4 L1
12.5 452 78 L9
15.6 86 52 L5
85.3 12 12 L4
12.5 35 36 L2
85.9 78 78 L3
78.5 44 44 L6
4.6 10 11 L7
13.9 157 2 L8
', header = TRUE)
library(ggplot2)
ggplot() +
geom_point(df, mapping = aes(x = Footprint, y = Local_Number, color = '1')) +
geom_point(df, mapping = aes(x = -Remote_Number, y = Local_Number, color = '2')) +
geom_text(df, mapping = aes(x = Footprint, y = Local_Number, label = Location), hjust = 0, vjust = 0) +
geom_text(df, mapping = aes(x = -Remote_Number, y = Local_Number, label = Location), hjust = 0, vjust = 0) +
scale_color_manual('Legend', labels = c('Footprint', 'Remote number'), values = c('blue', 'red')) +
labs(y = 'Local Number')
Created on 2022-10-14 with reprex v2.0.2
If you want to show it on only a positive axis you could the negative sign like this:
library(ggplot2)
ggplot() +
geom_point(df, mapping = aes(x = Footprint, y = Local_Number, color = '1')) +
geom_point(df, mapping = aes(x = Remote_Number, y = Local_Number, color = '2')) +
geom_text(df, mapping = aes(x = Footprint, y = Local_Number, label = Location), hjust = 0, vjust = 0) +
geom_text(df, mapping = aes(x = Remote_Number, y = Local_Number, label = Location), hjust = 0, vjust = 0) +
scale_color_manual('Legend', labels = c('Footprint', 'Remote number'), values = c('blue', 'red')) +
labs(y = 'Local Number')
Created on 2022-10-14 with reprex v2.0.2
Here two more suggestions for visualisation. This seems to be paired data - remote vs local number. That can be either represented as a scatter plot or as change.
Footprint can then be encoded as color. Thanks +1 to Quieten for the data.
library(tidyverse)
df <- read.table(text = 'Footprint Local_Number Remote_Number Location
10.4 45 4 L1
12.5 452 78 L9
15.6 86 52 L5
85.3 12 12 L4
12.5 35 36 L2
85.9 78 78 L3
78.5 44 44 L6
4.6 10 11 L7
13.9 157 2 L8
', header = TRUE)
df %>%
ggplot(aes(Local_Number, Remote_Number)) +
## use Number as x and y and color code by footprint value
geom_point(aes(color = Footprint), size = 3) +
## label the points, best with repel
ggrepel::geom_text_repel(aes(label = Location)) +
## optional add a line of equality to help intuitive recognition of change
## + keeping same limits helps intuitive comparison
geom_abline(intercept = 0, slope = 1, lty = 2, size = .3) +
coord_equal(xlim = range(c(df$Local_Number, df$Remote_Number)), ylim = range(c(df$Local_Number, df$Remote_Number))) +
## optional change color scale
scale_color_viridis_c(option = "magma")
## or, not to waste half of your graph (there is no positive value)
## you can show the difference instead
df %>%
mutate(change = Local_Number-Remote_Number) %>%
ggplot() +
## now use Location as x variable, therefore no labels needed any more
geom_point(aes(Location, change, color = Footprint), size = 3) +
## optional change color scale
scale_color_viridis_c(option = "magma")
Created on 2022-10-14 by the reprex package (v2.0.1)
I am trying to graph two different datasets, reconstructed temperatures (10-16) and charcoal data (0-140), with two different time series values, using ggplot. Is this possible?
I used this code (see below) but unfortunately it produced a plot (see below) that limits the variability of the temperature reconstruction. Is there a way to adjust the y axis so we can see more variability in the temperature record?
Thank you very much for your support.
R code
df <- data.frame(Charfiretempdata$AGETEMPS, Charfiretempdata$FIREAGE, Charfiretempdata$Comp2TEMPS,Charfiretempdata$Char.Acc.Rate..Char...cm.2.yr.1.)
ggplot(df) +
geom_col(mapping = aes(x = Charfiretempdata$FIREAGE,
y = Charfiretempdata$Char.Acc.Rate..Char...cm.2.yr.1. * 16/150), size = 2, color = "darkblue",
fill = "white") +
geom_line(mapping = aes(x = Charfiretempdata$AGETEMPS, y = Charfiretempdata$Comp2TEMPS)) +
geom_point(mapping = aes(x = Charfiretempdata$AGETEMPS, y = Charfiretempdata$Comp2TEMPS), size
= 3, shape = 21, fill = "white")+
scale_y_continuous(
name = expression("Temperature ("~degree~"C)"),
sec.axis = sec_axis(~ . * 150/16 , name = "Charcoal (mm)"))
R plot
I create a random sample data that would share similar characteristics to your data.
library(dplyr)
library(ggplot2)
set.seed(282930)
df <- tibble(x_axis = c(1400, 1500, 1600, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016, 2017),
y_axis_1 = runif(20, min = 10, max = 16),
y_axis_2 = runif(20, min = 0, max = 150))
Here is the df
> df
# A tibble: 20 x 3
x_axis y_axis_1 y_axis_2
<dbl> <dbl> <dbl>
1 1400 15.7 5.28
2 1500 11.8 141.
3 1600 14.5 149.
4 2000 11.6 121.
5 2001 15.6 37.3
6 2002 15.0 72.5
7 2003 10.7 130.
8 2004 15.4 84.7
9 2005 11.5 118.
10 2006 10.4 17.4
11 2007 11.3 124.
12 2008 13.6 22.6
13 2009 13.0 14.5
14 2010 15.9 142.
15 2011 12.3 103.
16 2012 10.3 131.
17 2013 12.6 93.6
18 2015 14.6 12.4
19 2016 11.4 27.9
20 2017 15.3 116.
Here is the ggplot similar to your but with the different Axis adjustment
ggplot(df,
# as they sharing same X-axis you can define share variable aes in the
# main call of ggplot
aes(x = x_axis)) +
geom_col(mapping =
# added 10 to 2nd axis value as will scale from 10 instead of 0
aes(y = (y_axis_2 * 10 / 150) + 10),
# the size here is size of the border - and due to the nature of
# your data, the col suppose to be very thin to match with that one
# tick on x-axis - so the inner fill is covered by dark blue border
size = 2, color = "darkblue",
# The fill is not really useful as you cannot see it.
fill = "white") +
geom_line(mapping = aes(y = y_axis_1)) +
geom_point(mapping = aes(y = y_axis_1), size
= 3, shape = 21, fill = "white") +
# Set the main Axis start at 10 instead of 0 so it would allow more zoom into it
coord_cartesian(ylim = c(10, 20), expand = c(0, 0)) +
scale_y_continuous(
name = expression("Temperature ("~degree~"C)"),
# The calculation of second axis lable is calculate base on 1st axis.
# and as the 1st axis start at 10, there fore the fomular need to minus 10
# before multiply back 15 - I keep 150 / 10 so it clear reverse of original
# transform of the 2nd axis value above.
sec.axis = sec_axis(~ (. - 10) * 150 / 10 , name = "Charcoal (mm)"))
Here is the sample output plot
And even with the adjsut y-axis we can hardly see the temperature at the end of the data because there are a lot more data points at the end. I think if you don't need all of data point at the end you may just take every 10 x as the data was on the range of 600 years so you don't need to graph so much details at the end. And if you need details just graph that time frame separately
Filter data at the end to only take every 10 year instead
ggplot(df %>% filter(x_axis <= 2000 | x_axis %% 10 == 0),
aes(x = x_axis)) +
# similar code to above but I use geom_bar instead
geom_bar(mapping =
aes(y = (y_axis_2 * 10 / 150) + 10),
stat = "identity", size = 2, color = "darkblue",
fill = "white") +
geom_line(mapping = aes(y = y_axis_1)) +
geom_point(mapping = aes(y = y_axis_1), size
= 3, shape = 21, fill = "white")+
scale_y_continuous(
name = expression("Temperature ("~degree~"C)"),
sec.axis = sec_axis(~ (. - 10) * 150/10 , name = "Charcoal (mm)")) +
coord_cartesian(ylim = c(10, 20), expand = c(0, 0))
(As you can see that with less data point, we started to see the fill as plot have more space)
Zoom in at the end of the data
ggplot(df %>% filter(x_axis >= 2000),
aes(x = x_axis)) +
# similar code to above but I use geom_bar instead
geom_bar(mapping =
aes(y = (y_axis_2 * 10 / 150) + 10),
stat = "identity", size = 2, color = "darkblue",
fill = "white") +
geom_line(mapping = aes(y = y_axis_1)) +
geom_point(mapping = aes(y = y_axis_1), size
= 3, shape = 21, fill = "white")+
scale_y_continuous(
name = expression("Temperature ("~degree~"C)"),
sec.axis = sec_axis(~ (. - 10) * 150/10 , name = "Charcoal (mm)")) +
coord_cartesian(ylim = c(10, 20), expand = c(0, 0))
(Now we can see both the darkblue border and the white fill inside)
I am trying to create individual plots facetted by 'iid' using 'facet_multiple', in the following dataset (first 3 rows of data)
iid Age iop al baseIOP baseAGE baseAL agesurg
1 1 1189 20 27.9 21 336 24.9 336
2 2 877 11 21.5 16 98 20.3 98
3 2 1198 15 21.7 16 98 20.3 98
and wrote the following code:
# Install gg_plus from GitHub
remotes::install_github("guiastrennec/ggplus")
# Load libraries
library(ggplot2)
library(ggplus)
# Generate ggplot object
p <- ggplot(data_longF1, aes(x = Age, y = al)) +
geom_point(alpha = 0.5) +
geom_point(aes(x= baseAGE, y=baseAL)) +
labs(x = 'Age (days)',
y = 'Axial length (mm)',
title = 'Individual plots of Axial length v time')
p1 <- p+geom_vline(aes(xintercept = agesurg),
linetype = "dotted",
colour = "red",
size =1.0)
p2<- p1 + geom_text(aes(label=iop ,hjust=-1, vjust=-1))
p3 <- p2 + geom_text(aes(label = baseIOP, hjust=-1, vjust=-1))
# Plot on multiple pages (output plot to R/Rstudio)
facet_multiple(plot = p3,
facets = 'iid',
ncol = 1,
nrow = 1,
scales = 'free')
The main issue I am having is labeling the points. The points corresponding to (x=age, y=axl) get labelled fine, but labels for the second group of points (x=baseIOP, y=baseAL) gets put in the wrong place.individual plot sample
I have had a look at similar issues in Stack Overflow e.g. ggplot combining two plots from different data.frames
But not been able to correct my code.
Thanks for your help
You need to define the x and y coordinates for the labels or they will default to the last ones specified.
Thus the geom_text() definitions should look something like:
data_longF1 <-read.table(header=TRUE, text="iid Age iop al baseIOP baseAGE baseAL agesurg
1 1 1189 20 27.9 21 336 24.9 336
2 2 877 11 21.5 16 98 20.3 98
3 2 1198 15 21.7 16 98 20.3 98")
# Generate ggplot object
p <- ggplot(data_longF1, aes(x = Age, y = al)) +
geom_point(alpha = 0.5) +
geom_point(aes(x= baseAGE, y=baseAL)) +
labs(x = 'Age (days)',
y = 'Axial length (mm)',
title = 'Individual plots of Axial length v time')
p1 <- p+geom_vline(aes(xintercept = agesurg),
linetype = "dotted",
colour = "red",
size =1.0)
#Need to specify the x and y coordinates or will default to the last ones defined
p2<- p1 + geom_text(aes(x=Age, y= al, label=iop ,hjust=-1, vjust=-1))
p3 <- p2 + geom_text(aes(x=baseAGE, y= baseAL, label = baseIOP, hjust=-1, vjust=-1))
print(p3)
How to show loess smoothed trend-line in the plot? Please help to handle the warning message: "Removed 19 rows containing non-finite values (stat_smooth)".
My data:
yrcnt<-read.table(header = TRUE, text = "year outcome pop rate pred.SC
1 1995 2306 87592001 2.632660 0.9626214
2 1996 2221 87628543 2.534562 0.9599941
3 1997 2202 81872629 2.689544 0.9573667
4 1998 2316 88200076 2.625848 0.9547394
5 1999 2456 96200312 2.553006 0.9521121
6 2000 2526 99565063 2.537035 0.9494848
7 2001 2511 95951330 2.616952 0.9468575
8 2002 2537 96976191 2.616106 0.9442302
9 2003 2618 101673130 2.574918 0.9416028
10 2004 2644 104554479 2.528825 0.9389755
11 2005 2594 100522055 2.580528 0.9363482
12 2006 2620 105787278 2.476668 0.9337209
13 2007 2722 108946407 2.498476 0.9310936
14 2008 2788 112200567 2.484836 0.9284663
15 2009 2706 104491560 2.589683 0.9258389
16 2010 2773 108651896 2.552187 0.9232116
17 2011 2764 109632577 2.521148 0.9205843
18 2012 2694 107594922 2.503836 0.9179570
19 2013 2673 107553219 2.485281 0.9153297")
http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html
My code:
p1 <- ggplot(yrcnt, aes(y = log(rate), x = year))
yrcnt$pred.SC <- predict(lm(year ~ log(rate), data = yrcnt))
p1 + geom_line(aes(color = rate)) +geom_line(aes(y = pred.SC))
p1 + geom_line(aes(color = rate)) + geom_smooth()
p4 <- p1 + geom_line(aes(color = rate)) + geom_smooth(color="red")
p4 + scale_x_continuous(name = "Years",limits = c(1995, 2013),breaks = 1994:2014) +
scale_y_continuous(name = "Pancreatic Cancer Hospitalization Rate, 1995-2013",limits = c(2.4, 2.7),breaks = seq(2.4, 2.7, by = 0.1)) +
ggtitle("Long Term Trend in Pancreatic Cancer Hospitalizations")
>`geom_smooth()` using method = 'loess'
p4=Base plot with trendline
Scale plot that was failed to get incorporated to base plot=p4
In the function call to scale_y_continuous(), remove the arguments
limits = c(2.4, 2.7),
breaks = seq(2.4, 2.7,
by = 0.1))
Because the true limits of the y-axis are between 0.9 and 1, and you are setting them to a range between 2.4 and 2.7 . I don't know if you need the rate or the log(rate) here.
An alternative would be
library('ggplot2')
p1 <- ggplot(yrcnt, aes(y = rate, x = year))
######### lm() args flipped, then
######### wrapped in exp() function.
yrcnt$pred.SC <- exp(predict(lm( log(rate) ~ year, data = yrcnt)))
p1 + geom_line(aes(color = rate)) +geom_line(aes(y = pred.SC))
p4 <- p1 + geom_line(aes(color = rate)) + geom_smooth(color="red", method="loess")
p4 + scale_x_continuous(name = "Years",limits = c(1995, 2013),breaks = 1994:2014) +
scale_y_continuous(name = "Pancreatic Cancer Hospitalization Rate, 1995-2013",limits = c(2.4, 2.7),breaks = seq(2.4, 2.7, by = 0.1)) +
theme(legend.position ="none") +
ggtitle("Long Term Trend in Pancreatic Cancer Hospitalizations")
I'm trying to offset two points in a ggplot scatter plot made with geom_points. I tried the default jitter as well as my own settings, but in both cases I don't see a difference in the plot. The plot always looks like the attached image, with Brazil and Mexico quite crowded together. What should I do differently?
Here's the code I've tried:
p = ggplot(morning_night, aes(x = ascent_median, y = duration_median)) + geom_point(position = "jitter", aes(color=type, shape=region, size=30))
+geom_text(aes(label=country), hjust = 0, vjust = 1.2)
p = ggplot(morning_night, aes(x = ascent_median, y = duration_median)) + geom_point(position = position_jitter(w=.1, h=.1), aes(color=type, shape=region, size=30))
+geom_text(aes(label=country), hjust = 0, vjust = 1.2)
p = ggplot(morning_night, aes(x = ascent_median, y = duration_median)) + geom_point(position = position_jitter(w=.1, h=.3), aes(color=type, shape=region, size=30))
+geom_text(aes(label=country), hjust = 0, vjust = 1.2)
Here's the data frame used for this graph:
country ascent_median duration_median type region
Sweden 41.6 1793 morning Scandinavia
Denmark 33 1960 night Scandinavia
Mexico 75 1877 morning LatinAmerica
Brazil 74.4 1928 night LatinAmerica
Indonesia 41.8 1492 morning SoutheastAsia
Malaysia 48.7 2208 night SoutheastAsia
Switzerland 64.3 2461 morning CentralEurope
Austria 67.9 2113 night CentralEurope