R how to add linear trendline based on summary database - r

I am attempting to make a linear trendline based on a summary database with calculated means and standard errors. The idea is I want to have 4 points each representing a high or low shelter module at two different study sites (Waikiki and Hanauma Bay).
Database
data <- structure(list(Site_long = structure(c(1L, 1L, 2L, 2L), .Label = c("Hanauma Bay",
"Waikiki"), class = "factor"), Shelter = c("High", "Low", "High",
"Low"), mean_urchin = c(408.408115828422, 140.379098168879, 13.52445484726,
4.94269044271269), mean_algae = c(0.823598077528884, 0.90810903848719,
0.907753323551643, 0.89177862643068), standard_error_urchin = c(72.3961797019843,
27.120064093754, 8.47543198370478, 2.29511348616285), standard_error_algae = c(0.0242715527026854,
0.0109737470337568, 0.00941191528094468, 0.0122105898310865),
lower_urchin = c(336.011936126438, 113.259034075125, 5.04902286355523,
2.64757695654983), upper_urchin = c(480.804295530406, 167.499162262633,
21.9998868309648, 7.23780392887554), lower_algae = c(0.799326524826198,
0.897135291453433, 0.898341408270698, 0.879568036599593),
upper_algae = c(0.847869630231569, 0.919082785520947, 0.917165238832587,
0.903989216261766)), row.names = c(NA, -4L), groups = structure(list(
Site_long = structure(1:2, .Label = c("Hanauma Bay", "Waikiki"
), class = "factor"), .rows = structure(list(1:2, 3:4), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
When I add a geom_smooth() to my plot code, it does not render a trendline at all.
Graph code
ggplot(data = data, aes(x = mean_urchin, y = mean_algae, fill = Shelter, shape = Site_long)) +
geom_point(aes(size = 3)) +
stat_smooth(method=lm) +
scale_shape_manual(values = c(21, 24)) +
scale_fill_manual(values = c(NA, "black"), guide = guide_legend(override.aes = list(shape = 21))) +
geom_errorbar(aes(ymin = lower_algae, ymax = upper_algae), position = position_dodge(.8), width = .1) +
geom_errorbarh(aes(xmin = lower_urchin, xmax = upper_urchin), position = position_dodge(.8), width = .1) +
labs(x = "Mean urchin biomass (Kg) ± SEM", y = "Mean benthic algal cover ± SEM")
Is it possible to render a trendline based on a summary database like this or do I have to link it to the raw database? I attempted to use the raw data to generate the trendline but then ended up with 4 separate trendlines which is not what I want.
Any insight or advice would be appreciated. Thanks in advance!

Related

Add bar plot in a bar plot (ggplot2)

I would like create a plot like this:
i.e. i would like to add another bar plot inside in my bar plot basic,
whould could i do something like this ?
i have no idea about how create this,
data:
structure(list(Impacted_sector = structure(c(3L, 3L, 1L, 1L,
5L, 2L, 4L), .Label = c("Authorities-Stakeholders", "Public and social welfare",
"Agriculture", "Variety", "Environment"), class = "factor", scores = structure(c(Agriculture = -4.49129192,
`Authorities-Stakeholders` = -3125.027684115, Environment = -0.33176146,
`Public and social welfare` = -15.46511976, Variety = -0.39712811
), .Dim = 5L, .Dimnames = list(c("Agriculture", "Authorities-Stakeholders",
"Environment", "Public and social welfare", "Variety")))), Type_of_cost_merged = structure(c(2L,
1L, 1L, 3L, 1L, 2L, 1L), .Label = c("Management", "Damage", "Mixed"
), class = "factor", scores = structure(c(Damage = -7.803309445,
Management = -1564.7958562425, Mixed = -0.44191754), .Dim = 3L, .Dimnames = list(
c("Damage", "Management", "Mixed")))), cost = c(141499.13,
8841084.71, 6249613450.69, 441917.54, 331761.46, 15465119.76,
397128.11), Million = c(0.14149913, 8.84108471, 6249.61345069,
0.44191754, 0.33176146, 15.46511976, 0.39712811)), row.names = c(NA,
-7L), groups = structure(list(Impacted_sector = structure(1:5, .Label = c("Authorities-Stakeholders",
"Public and social welfare", "Agriculture", "Variety", "Environment"
), scores = structure(c(Agriculture = -4.49129192, `Authorities-Stakeholders` = -3125.027684115,
Environment = -0.33176146, `Public and social welfare` = -15.46511976,
Variety = -0.39712811), .Dim = 5L, .Dimnames = list(c("Agriculture",
"Authorities-Stakeholders", "Environment", "Public and social welfare",
"Variety"))), class = "factor"), .rows = structure(list(3:4,
6L, 1:2, 7L, 5L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
All help is appreciate
As I mentioned in my comment one option would be to make use of patchwork::inset_element. As in your data the cost for Authorities-Stakeholders differs heavily from the cost of the other categories I opted for adding Authorities-Stakeholders as an inset plot.
library(ggplot2)
library(patchwork)
library(dplyr)
p1 <- dd %>%
filter(!Impacted_sector == "Authorities-Stakeholders") %>%
ggplot(aes(Impacted_sector, cost, fill = Type_of_cost_merged)) +
geom_col() +
scale_y_continuous(labels = scales::number_format(scale = 1e-6), expand = c(0, .05)) +
theme_minimal() +
guides(fill = guide_legend(override.aes = list(color = "black"))) +
labs(fill = "Type of cost")
p2 <- dd %>%
filter(Impacted_sector == "Authorities-Stakeholders") %>%
ggplot(aes(Impacted_sector, cost, fill = Type_of_cost_merged)) +
geom_col() +
scale_y_continuous(labels = scales::number_format(scale = 1e-6), expand = c(0, .05)) +
theme_minimal() +
theme(plot.background = element_rect(fill = "white", color = NA)) +
guides(fill = "none") +
labs(x = NULL, y = NULL)
p1 + patchwork::inset_element(p2, .6, .6, 1, 1)

gganimate error: Error in seq.default(range[1], range[2], length.out = nframes) : 'from' must be a finite number

I am trying to make an animated cumulative map plot of the following data:
structure(list(station_install_date = structure(c(16684, 16684,
16684, 16684, 16684, 16684), class = "Date"), lat = c(37.548645,
37.549561, 37.550007, 37.550629, 37.552746, 37.554951), long = c(126.912827,
126.905754, 126.914825, 126.914986, 126.918617, 126.910835),
capa_sum = c(10L, 5L, 5L, 13L, 10L, 14L)), row.names = c(NA,
-6L), groups = structure(list(station_install_date = structure(c(16684,
16684, 16684, 16684, 16684, 16684), class = "Date"), lat = c(37.548645,
37.549561, 37.550007, 37.550629, 37.552746, 37.554951), .rows = structure(list(
1L, 2L, 3L, 4L, 5L, 6L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
My code looks as below:
seoul <- get_googlemap("Seoul, South Korea", zoom=11, maptype = "roadmap")
ggmap(seoul) +
geom_point(data = install_time_df, aes(x = long, y = lat), color = "red", alpha = 0.3) +
transition_time(station_install_date) +
ease_aes("linear")
However, I keep getting the error:
Error in seq.default(range[1], range[2], length.out = nframes) :
'from' must be a finite number
Try:
p2<- ggmap(seoul) +
geom_point(data = install_time_df, aes(x = long, y = lat), color = "red", alpha = 0.3) +
transition_manual(station_install_date, cumulative = TRUE) +
ease_aes("linear")
anim2<- animate(p2, renderer = gifski_renderer())
anim_save("C:\\Users\\82104\\Desktop\\따릉이\\anim2.gif", animation = anim2)

Plotting yearly/area comparison in ggplot in R through dodging [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following dataset showing when a person (denoted by variable id) synced data to the server in districts A and B. I already computed counts and percentages by district and year.
This is my summarized data:
df3 <- structure(list(district = c("A", "A", "B", "B"), year_sync = c(
2019L,
2020L, 2019L, 2020L
), ssum = c(32L, 32L, 33L, 33L), n = c(
7L,
25L, 25L, 8L
), percent = c(
0.21875, 0.78125, 0.757575757575758,
0.242424242424242
), label = c("21.88%", "78.12%", "75.76%", "24.24%")), row.names = c(NA, -4L), groups = structure(list(district = c(
"A",
"B"
), .rows = structure(list(1:2, 3:4), ptype = integer(0), class = c(
"vctrs_list_of",
"vctrs_vctr", "list"
))), row.names = 1:2, class = c(
"tbl_df",
"tbl", "data.frame"
), .drop = TRUE), class = c(
"grouped_df",
"tbl_df", "tbl", "data.frame"
))
I need to create a ggplot (like the one shown below).
This is my plotting code:
ggplot(df3, aes(y=ssum, x=factor(year_sync), fill=district)) +
geom_bar(stat='identity',
#color='black',
position = position_dodge(width=0.8), width=0.8) +
geom_text(aes(label = label),
position = position_dodge(width=0.8),
size = 3) +
xlab ("Year") +
ylab ("Number of People") +
scale_fill_manual(values=c("aquamarine4", "bisque3")) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, face = "italic"))
However, the second part of the code gives me the following error: Error in unique.default(x, nmax = nmax) : unique() applies only to vectors.
Can anyone please help in telling what am I doing wrong as I am new to R.
Looking forward to solving this!
Thank you
Simply replacing year with year_sync, countby n and mapping n on the y aes (instead of ssum) gives the wanted plot without any error messages:
df3 <- structure(list(district = c("A", "A", "B", "B"), year_sync = c(
2019L,
2020L, 2019L, 2020L
), ssum = c(32L, 32L, 33L, 33L), n = c(
7L,
25L, 25L, 8L
), percent = c(
0.21875, 0.78125, 0.757575757575758,
0.242424242424242
), label = c("21.88%", "78.12%", "75.76%", "24.24%")), row.names = c(NA, -4L), groups = structure(list(district = c(
"A",
"B"
), .rows = structure(list(1:2, 3:4), ptype = integer(0), class = c(
"vctrs_list_of",
"vctrs_vctr", "list"
))), row.names = 1:2, class = c(
"tbl_df",
"tbl", "data.frame"
), .drop = TRUE), class = c(
"grouped_df",
"tbl_df", "tbl", "data.frame"
))
library(ggplot2)
ggplot(df3, aes(y = n, x = factor(year_sync), fill = district)) +
geom_bar(
stat = "identity",
color = "black",
position = position_dodge(width = 0.8), width = 0.8
) +
geom_text(aes(label = label, y = n),
position = position_dodge(width = 0.8),
size = 3, vjust = -1
) +
xlab("Year") +
ylab("Number of People") +
scale_fill_manual(values = c("aquamarine4", "bisque3")) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, face = "italic")
)

Can I add a curved trend line to a ggplot with only one data point per column?

I have used ggplot2 to create a line graph for a soil water release curve. However, because I only have one data point at each pressure value (the x axis), the lines are connected directly from point to point. I would like to keep the points but have a curve that shows the trend of the points. This is the typical style for soil water release curves.
Data:
> dput(head(sub2018))
structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label =
c("2018",
"2019"), class = "factor"), Pressure = structure(1:6, .Label = c("-1",
"-0.5", "-0.25", "-0.2", "-0.1", "-0.05", "-0.02", "-0.01", "0"
), class = "factor"), meanVWC = c(0.291819594, 0.308328767666667,
0.318496127666667, 0.323671866333333, 0.349356212666667,
0.374201803666667
)), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), vars = "Year", drop = TRUE, indices = list(
0:5), group_sizes = 6L, biggest_group_size = 6L, labels = structure(list(
Year = structure(1L, .Label = c("2018", "2019"), class = "factor")),
row.names = c(NA,
-1L), class = "data.frame", vars = "Year", drop = TRUE))
ggplot:
GGplot2018 <- ggplot(sub2018, aes(x=Pressure, y=meanVWC, group=1)) +
geom_line() +
geom_point() + labs(y= "Volumetric Water Content")
GGplot2018
Does anyone know if/how I can add this curve?
Thanks very much for any help!
I believe this is what you are looking for:
GGplot2018 <- ggplot(sub2018, aes(x=Pressure, y=meanVWC, group=1)) +
geom_line()+
geom_point() + labs(y= "Volumetric Water Content")+
geom_smooth(method = "lm",se = FALSE)
GGplot2018

ggplot invalid argument to unary operator

I am getting the below error msg when running ggplot pie chart...any idea what issue could be?
code is :
ggplot(pie_unrated, aes(x = "FEBRUARY IBG UNRATED Book COMPOSITION", y = prop,
fill = ProductDetails)) + geom_bar(width = 1,
stat = "identity", color = "white")
+ coord_polar(theta = "y", start = 0) + ggpubr::fill_palette("jco")
+theme_void()
My error Msg :
Error in +coord_polar(theta = "y", start = 0) :
invalid argument to unary operator
>
dput(head(pie_unrated)
structure(list(RatingStatus = c("UNRATED", "UNRATED", "UNRATED",
"UNRATED", "UNRATED", "UNRATED"), ProductDetails = structure(c(1L,
2L, 6L, 7L, 9L, 10L), .Label = c("ACB", "Bonds", "Cash and Short Term",
"Deposit with Banks", "LBD", "LC", "LG", "loan", "Loan", "OD",
"Treasury Bonds"), class = "factor"), counts = c(10L, 1L, 21L,
102L, 758L, 126L), prop = c(1, 0.1, 2.1, 10, 74.5, 12.4), lab.ybos = c(0.5,
1.05, 2.15, 8.2, 50.45, 93.9)), .Names = c("RatingStatus", "ProductDetails",
"counts", "prop", "lab.ybos"), row.names = c(NA, -6L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = "RatingStatus", drop = TRUE, indices = list(
0:5), group_sizes = 6L, biggest_group_size = 6L, labels = structure(list(
RatingStatus = "UNRATED"), row.names = c(NA, -1L), class = "data.frame", vars = "RatingStatus", drop = TRUE, .Names = "RatingStatus"))
library(ggpubr)
ggplot(pie_unrated,
aes(x = "FEBRUARY IBG UNRATED Book COMPOSITION", y = prop, fill = ProductDetails)) +
geom_bar(width = 1,stat = "identity", color = "white") +
coord_polar(theta = "y", start = 0) + ggpubr::fill_palette("jco") + theme_void()

Resources