PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, April 23, 2022

[FIXED] How to plot ggplot2 scatter plot with dodged points OR points that are pie-chart?

 April 23, 2022     ggplot2, pie-chart, r     No comments   

Issue

I have 4 species found in 5 samples in a plot. I want to visualize them in ggplot2 with latitude on x axis, longitude on y-axis; and their abundance essentially as the size coloured by species. I tried to work and got the following

lat <- c(1,2,3,4,5)
long <- c(13,2,7,5,15)
sp1 <- c(0,10,4,3,2)
sp2 <- c(20,10,14,0,1)
sp3 <- c(10,0,2,3,7)
sp4 <- c(2,0,0,0,12)

df <- data.frame(lat, long, sp1, sp2, sp3, sp4)
library(tidyr)
df.long <- gather(df,
                     key = "species",
                     value = "abundance",
                    sp1, sp2, sp3, sp4)

enter image description here

I want to know, how can I stagger this points to be represented as in the left side of the figure below? Alternately, I'm trying to also plot pie charts on those points where the size of PIE reflects the overall abundance. I don't even know if this is possible in R..but I'm shooting my shot here. Any help would be welcome!

enter image description here


Solution

If you want to use a dodged position, set position = position_dodge() and specify a width:

library(ggplot2)
ggplot(df.long, aes(x = lat, y = long, color = species, size = abundance)) +
  geom_point(position = position_dodge(width = 0.5)) 

enter image description here



Answered By - Gregor Thomas
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing