[R] Circular plot - polar plot code questions

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Mon Apr 24 14:03:28 CEST 2023


1) If you do not need it do not plot it. However, also consider how others will use your content. Might it be a trivial piece of information for you, but a critical piece of information for someone trying to use your content. A meta analysis, or just wanting to try to relate your outcomes to the results from their experiment.

2) There would be no need of melt() if the data is already in the proper format. The long format is not always the right format, though more often than not it is the right format (at least in my field of study).

3) google search something like "x-axis line weight ggplot" or something like that. There are excellent online resources to answer focused questions like that.

4) This might help: https://www.datanovia.com/en/blog/ggplot-colors-best-tricks-you-will-love/



Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Bruce Miller
Sent: Sunday, April 23, 2023 1:19 PM
To: r-help using r-project.org
Subject: [R] Circular plot - polar plot code questions

[External Email]

Hi all...
I assume there are a host of GGplot2 users out there.
I have circular plot - polar plot code questions I needed to create circular - polar plots of reproductive status for bats.  I found a great sample of how to do this here:
https://stackoverflow.com/questions/41081376/how-to-create-a-circular-plot-showing-monthly-presence-or-absence-using-radial-p

I modified the sample code to meet the 3 data elements I am using the "raw data" table below with T=testes enlarge, P= Pregnant and L= Lactating.

library(data.table)


m <- fread("id    T    month    P    L
1    0    1    0    0
2    0    2    0    0
3    0    3    0    0
4    1    4    0    0
5    1    5    1    0
6    1    6    1    1
7    0    7    1    1
8    0    8    1    1
9    0    9    0    1
10    0    10    0    0
11    0    11    0    0
12    0    12    0    0")
# reshape from wide to long (as preferred by ggplot) ml <- melt(m, measure.vars = c("T", "P", "L"))

# create factors to ensure desired order ml[, variable := factor(variable, levels = c("T", "P","L"))]

ml[, fct_month := factor(month, levels = 1:12, labels = month.abb)]
library(ggplot2)
ggplot(ml[value != 0], aes(x = fct_month, y = variable,
                            group = variable, colour = variable)) +
   geom_line(size = 3) +
   scale_y_discrete(expand = c(0, 1), breaks = NULL) +
   xlim(month.abb) +
   coord_polar() +
   theme_bw() + xlab(NULL) + ylab(NULL)

This is creating a plot more or less as needed.  4 questions:
1 Do I even need the ID field? Seems not useful.
2 I assume the melt step can be avoided if my data is "Tidy" and long format at the start, correct?
3  How can I increase the weight of the month lines and labels
4  Where can I add color choices for the 3 variables in the plot? Simply adding an HTML color number to the "colour=variable" then plots the data and labeling one value as that color.

Tnx all. The list is always educational on a daily basis.
Cheers,
Bat Dude
        [[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.r-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list