ericpante / marmap

Import, plot and analyze bathymetric and topographic data
32 stars 7 forks source link

Error when plotting autoplot + geom_scatterpie #20

Closed IssamHu closed 3 years ago

IssamHu commented 3 years ago

Hi Sir, I get this error when I plot autoplot + geom_scatterpie:

Error: Discrete value supplied to continuous scale

GCC <- getNOAA.bathy(lon1 = 48 , lon2 = 60,
                     lat1 = 15, lat2 = 31, res = 1, keep = TRUE)
autoplot(GCC, geom=c("r", "c"), colour="white", size=0.1) + 
  scale_fill_etopo()  +  
  coord_cartesian(expand = 0) + 
  labs(y = "Latitude", x = "Longitude") +
  theme(legend.position = "none") + 
  geom_scatterpie(aes(x=Longitude, y=Latitude, group=Country), data=Piedata,
                  cols=c("Legal", "Illegal"), pie_scale = 1, 
                  sorted_by_radius = FALSE, legend_name = "type",
                  long_format=FALSE)

the data: Piedata Country Legal Illegal Latitude Longitude 1 A 0.375 0.625 29.74247 48.41323 2 B 0.852 0.148 27.77500 49.14698 3 C 0.345 0.655 26.31520 50.49300 4 D 0.562 0.439 25.56074 51.64831 5 E 0.410 0.590 25.45679 54.72089 6 F 0.750 0.250 21.11154 59.32937

Please how can I get the plot Thanks IssamHu

besibo commented 3 years ago

Once again, please provide a reprex!

Here again, this is not a marmap problem/bug/issue. The error comes from a conflict between the fill colour palette you provide for the tiles of the base map (here, this is a continuous variable: for each depth value, a colour is associated), and the fill colour used for the pie charts (here, this is a discrete variable: for each level of your factor, Legal or Illegal, a fill colour is used) . This has nothing to do with marmap: this is the normal behaviour for any type of ggplot plot.

In your specific case, the following code works, but you lose the terrain colors for the base map:

autoplot(GCC, geom=c("c")) + 
  labs(y = "Latitude", x = "Longitude") +
  theme(legend.position = "none") + 
  geom_scatterpie(data=Piedata,, aes(x = Longitude, y = Latitude, group = Country), 
                  cols = c("Legal", "Illegal"), pie_scale = 1, 
                  sorted_by_radius = FALSE, legend_name = "type",
                  long_format = FALSE)

If you want more control over the aspect of the map, you'll need to build it manually (i.e. not by using marmap::autoplot()). A good example of how to do this with ggplot() is detailed here. You'll also probably need to explore how you can specify multiple fill scales for specific layers of a ggplot by looking at ?ggplot2::guides, because yes, you can indeed get 2 fill scales on the same plot: one for continuous variables, and one for discrete variables. But you will need to specify them explicitly.

IssamHu commented 3 years ago

Pleas for give me for inconvenience The data I used

GCC <- getNOAA.bathy(lon1 = 48 , lon2 = 60,
                     lat1 = 15, lat2 = 32, res = 1, keep = TRUE)
Piedata <- data.frame(Country = c("A", "B", "C", "D", "E", "F"),
                      Legal=c(0.375, 0.852, 0.345, 0.562, 0.410, 0.750), 
                      Illegal=c(0.625, 0.148, 0.655, 0.439, 0.590, 0.250), 
                      Latitude=c(29.742475, 27.774998,
                                26.315202, 25.560742, 25.45679, 21.111544),
                      Longitude=c(48.413228, 49.146977, 50.493004, 51.64831,
                                  54.72089, 59.329374),
                      cola= rep("lightblue",6),
                      colb=rep("orange",6))

Please I want to use autoplot because it gives better plot

IssamHu commented 3 years ago

I've used reprex and that what I get:

reprex() First three lines of putative code are:


GCC <- getNOAA.bathy(lon1 = 48 , lon2 = 60,
lat1 = 15, lat2 = 32, res = 1, keep = TRUE)
which doesn't look like R code.
Are we going in circles? Did you just run reprex()?
In that case, the clipboard now holds the *rendered* result.
Carry on with this reprex?
1: yes
2: no

Selection: 1 Putative code contains lines that start with #>. Are we going in circles? Did you just run reprex(..., venue = "r")? In that case, the clipboard now holds the rendered result. Carry on with this reprex? 1: yes 2: no

Selection: 1 Rendering reprex... Error: callr subprocess failed: attempt to use zero-length variable name Type .Last.error.trace to see where the error occured

besibo commented 3 years ago

Again, this is not a marmap issue. You need to learn more about how to use packages, online help (and "reprexes") and ggplot2 and this is clearly not the place for that. I'm sorry I can't help you.