Plotting relief data from tif file with other layers using ggplot

I’m (very) new to R. I’m trying to produce a map of the world with the following features:

  1. ISEA3H grid (with a spacing of 6 miles between hexagons (I don’t have enough memory to generate one with 6 mile hexagons; the closest I’ve gotten is 24 mile hexagons)).
  2. No political borders (i.e., only “natural borders, like coastlines, lakes, rivers, etc.) I’ve got this down as well.
  3. Elevation/relief data. This is where I’m having trouble.
  4. The cell number printed inside the cells. I haven’t gotten this far yet.

Specifically, the problem I’m having is I can’t seem to “mesh” the data from my tif file with my map of the world.

I’m using the dggridR package, which produces an ISEA3H grid of the world with variable spacing. I’ve gotten pretty far.

Here’s my code thus far:

library(rgdal) library(sp) library(dggridR) library(mapdata) library(rnaturalearth) library(rnaturalearthdata) library(raster)  # I've increased the spacing to 500 miles so the map doesn't take so long to render between iterations dggs <- dgconstruct(projection = "ISEA", aperture = 3, topology = "HEXAGON", precision = 7, spacing = 500, metric = FALSE)  # Save a fairly large image at the end of it jpeg("mymap.jpg", width = 9000, height = 6000, quality = 95)  # I'm using the physical data from https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/10m_physical.zip  # and the raster data from https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/raster/NE2_HR_LC_SR_W.zip  world <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_land") rivers <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_rivers_lake_centerlines") ocean <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_ocean") lakes <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_lakes") coast <- readOGR("/home/mario/Downloads/data/10m_physical", "ne_10m_coastline")  rel <- raster("/home/mario/Downloads/data/NE2_HR_LC_SR_W/NE2_HR_LC_SR_W.tif") rel_spdf <- as(rel, "SpatialPixelsDataFrame") rel <- as.data.frame(rel_spdf)  # use grid of the entire earth sa_grid <- dgearthgrid(dggs,frame=TRUE, wrapcells=TRUE)  # actually plot the data p<- ggplot() +      # what should be the relief layer     geom_tile(data = rel, aes(x = "x", y = "y")) +      # the world     geom_polygon(data=world, aes(x=long, y=lat, group=group), fill=NA, color="black") +      # lakes     geom_polygon(data=lakes, aes(x = long, y = lat, group = group), fill = '#ADD8E6') +      # more lakes     geom_path(data=lakes, aes(x = long, y = lat, group = group), color = 'blue') +      # rivers     geom_path(data=rivers, aes(x=long, y=lat, group=group), alpha=0.4, color="blue") +      # coastline     geom_path(data=coast, aes(x = long, y = lat, group = group), color = 'blue') +      # hexagonal grid     geom_polygon(data=sa_grid, aes(x=long, y=lat, group=group), fill="white", alpha=0.4) +      # hexagonal grid     geom_path(data=sa_grid, aes(x=long, y=lat, group=group), alpha=0.4, color="grey") +      # some necessary r code that I don't understand     coord_equal()  # change from flat map to globe p+coord_map("ortho", orientation = c(41.0082, 28.9784, 0))+   xlab('')+ylab('')+   theme(axis.ticks.x=element_blank())+   theme(axis.ticks.y=element_blank())+   theme(axis.text.x=element_blank())+   theme(axis.text.y=element_blank())+   ggtitle('World map')  # finish writing to jpeg dev.off() 

This is as far as I’ve gotten: https://imgur.com/y9LPqVS

The code above is currently chugging away, and has been for the past 4 hours. It’s remained within the bounds of the memory of my machine, so that’s a good sign.

What is the idiomatic way of projecting a geodesic grid over a relief map of the Earth? How could I include the relief data from the naturalearth tif file with my code thus far?

ggplot define monthly increments on x axis with multi year data

Hello I would like to create a plot with abbreviated month labels on the x-axis.

This is my data:

df <- structure(list(site = structure(c(33L, 34L, 35L, 36L, 37L, 38L,  39L, 40L, 41L, 42L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,  42L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 33L, 34L,  35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 33L, 34L, 35L, 36L, 37L,  38L, 39L, 40L, 41L, 42L), .Label = c("INDU01", "INDU02", "INDU03",  "INDU04", "INDU05", "INDU06", "INDU07", "INDU08", "INDU09", "INDU10",  "MISS01", "MISS02", "MISS03", "PIRO01", "PIRO02", "PIRO03", "PIRO04",  "PIRO05", "PIRO06", "PIRO07", "PIRO08", "PIRO09", "PIRO10", "SLBE03",  "SLBE04", "SLBE08", "SLBE09", "SLBE11", "V1DA1", "V3DA2", "V8DC2",  "VNPBP70", "APIS01", "APIS02", "APIS03", "APIS04", "APIS05",  "APIS06", "APIS07", "APIS08", "APIS10", "APIS11", "MISS04", "MISS08",  "MISS09", "SLBE01", "SLBE02", "SLBE05", "SLBE06", "SLBE07", "V12DA2",  "V4DC1", "V9DB1", "V9DC2", "VNPBP29", "VNPBP42", "GRPO02", "GRPO03",  "GRPO04", "GRPO05", "GRPO06", "GRPO07", "ISRO01", "ISRO02", "ISRO03",  "ISRO04", "ISRO05", "ISRO06", "ISRO07", "ISRO08", "ISRO09", "ISRO10",  "MISS05", "MISS06", "MISS07", "MISS10", "V10DA4", "V11DB4", "V12DC3",  "V5DA1", "V7DE2", "V7DF2", "GRPO01"), class = "factor"), log.start = structure(c(16218,  16218, 16218, 16218, 16218, 16218, 16218, 16218, 16218, 16218,  16540, 16541, 16550, 16553, 16542, 16543, 16542, 16553, 16553,  16552, 16905, 16905, 16905, 16906, 16905, 16905, 16905, 16905, 16905, 16905, 17270, 17270, 17290, 17290, 17289, 17289, 17291,  17288, 17290, 17290, 17622, 17622, 17655, 17655, 17655, 17655,  17655, 17655, 17655, 17655), class = "Date"), log.end = structure(c(16278,  16271, 16317, 16317, 16271, 16280, 16317, 16317, 16277, 16317,  16664, 16694, 16666, 16667, 16671, 16658, 16647, 16660, 16670,  16667, 17048, 17046, 17010, 17124, 17108, 17098, 17124, 17145,  17008, 17000, 17274, 17379, 17382, 17389, 17459, 17406, 17389,  17459, 17389, 17378, 17780, 17756, 17785, 17784, 17784, 17784,  17756, 17784, 17785, 17713), class = "Date"), year = c("2014",  "2014", "2014", "2014", "2014", "2014", "2014", "2014", "2014",  "2014", "2015", "2015", "2015", "2015", "2015", "2015", "2015",  "2015", "2015", "2015", "2016", "2016", "2016", "2016", "2016",  "2016", "2016", "2016", "2016", "2016", "2017", "2017", "2017",  "2017", "2017", "2017", "2017", "2017", "2017", "2017", "2018",  "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",  "2018"), start.month.day = c("05-28", "05-28", "05-28", "05-28",  "05-28", "05-28", "05-28", "05-28", "05-28", "05-28", "04-15",  "04-16", "04-25", "04-28", "04-17", "04-18", "04-17", "04-28",  "04-28", "04-27", "04-14", "04-14", "04-14", "04-15", "04-14",  "04-14", "04-14", "04-14", "04-14", "04-14", "04-14", "04-14",  "05-04", "05-04", "05-03", "05-03", "05-05", "05-02", "05-04",  "05-04", "04-01", "04-01", "05-04", "05-04", "05-04", "05-04",  "05-04", "05-04", "05-04", "05-04"), end.month.day = c("07-27",  "07-20", "09-04", "09-04", "07-20", "07-29", "09-04", "09-04",  "07-26", "09-04", "08-17", "09-16", "08-19", "08-20", "08-24",  "08-11", "07-31", "08-13", "08-23", "08-20", "09-04", "09-02",  "07-28", "11-19", "11-03", "10-24", "11-19", "12-10", "07-26",  "07-18", "04-18", "08-01", "08-04", "08-11", "10-20", "08-28",  "08-11", "10-20", "08-11", "07-31", "09-06", "08-13", "09-11",  "09-10", "09-10", "09-10", "08-13", "09-10", "09-11", "07-01" )), row.names = c(NA, -50L), class = "data.frame") 

And when I use ggplot…

library (ggplot2) p <- ggplot(df, aes(y=year)) p <- p + geom_segment(aes(x=start.month.day,xend=end.month.day,y=year,yend=year),size=2) p <- p + facet_wrap(~site,ncol=2) 

I receive the following plot…

enter image description here

I would like abbreviated monthly labels on the x-axis such as ‘Apr’ ‘Mar’ ‘Jun’, etc.

Thanks

Sample size, P-values (its relationship), and data visualization with plots. ggplot and T test

We know that P-values (within t-test context as an example..) is highly sensitive to sample size. A larger sample will yield a smaller p-value remaining everything else constant. On the other hand, Cohen´s d effect size remains the same.

Sample size and P values

I’m inspired in this code here, but I´ve changed some parts to make the difference between means constant, instead of creating a random variable based on a normal distribution.

Although everything is working, I do imagine that some of the experts in this community could improve my syntax.

library(tidyverse)  ctrl_mean <- 8 ctrl_sd <- 1  treated_mean <- 7.9 treated_sd <- 1.2  sample <- numeric() #criar vetor para grupar resultados nsim <- 1000 #criar variavel t_result <- numeric()  for (i in 1:nsim) {    set.seed(123)    t_result[i] <- (mean(ctrl_mean)-mean(treated_mean))/sqrt((ctrl_sd^2/(i))+(treated_sd^2/(i))) #manual t test   sample[i] <- i # number of participants } ds <- data.frame(   sample = sample, #assign the sample size   t_result = round(t_result,3), #get the t test result   degrees = sample*2-2) #compute the degrees of freedom  ds %>%    filter(sample>1) %>%    mutate(P_Value = 2*pt(abs(t_result), df=degrees,lower.tail=FALSE)) %>%    left_join(ds,.) -> ds  #plot  ggplot(ds, aes(x=sample, y=P_Value)) +   geom_line() +   annotate("segment", x = 1, xend=sample, y = 0.05, yend = 0.05, colour = "purple", linetype = "dashed") +   annotate("segment", x = 1, xend=sample, y = 0.01, yend = 0.01, colour = "red", linetype = "dashed") +   annotate("text", x = c(1,1), y=c(.035,.001), label = c("p < 0.05", "p < 0.01")) 

Gráfico pelo ggplot no RStudio v 1.1.463 não lê o subtitle e o caption em labs()

Olá. Estou com o problema relatado no título e não entendo por que no RStudio o subtitle e o caption não são lidos e quando replico o códio no Visual Studio ele carrega corretamente todos os comandos – o gráfico fica exatamente como está no código. Segue o código e um trecho dos dados para replicar no RStudio:

ggplot(Endividamento_Familias_Mensal, aes(Data, Endividamento_Familias)) + geom_line() + labs(title = “Endividamento Familiar”, caption = “Fonte: PNADC – Bacen”) Os dados são: inserir a descrição da imagem aqui

Este mesmo código, com estes mesmos dados, executados pelo Visual Studio gera o gráfico corretamente.

Veja como fica pelo RStudio: inserir a descrição da imagem aqui

Don’t have the expected figure with ggplot()

I have a data frame StatMNS StatMNS I plot statistic data (y) for each type of MNS (color) on different areas (x)

I run this:code

Firstly, I would like to show the legend Secondly, I would like replace the x vector 1:n by the name of the area (column readerName of the dataFrame)

I try rearranging my data in StatMNS2 StatMNS2 but I have error (Discrete value supplied to continuous scale)

Thanks for you help