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")
)
Related
I am having difficulty customising the ggplot (geom_point) output.
Points to have black outline but with different fill colors by group (white and black)
Is there a way to combine the legend into one?
Data
library(dplyr)
library(ggplot2)
dat <- structure(list(q = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L),
is_female = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), levels = c("Male", "Female"), class = "factor"), mean_exp = c(19.3095475534936,
19.2351713991988, 21.6718804471611, 21.69517120871, 23.4144749290445,
23.4191383190372, 25.2817658487443, 25.2772340355605, 28.3982168611512,
28.7869521340185), mean_fi = c(0.0915386254018914, 0.0912295567094683,
0.0771055282779973, 0.0790597510143077, 0.0859508568981647,
0.088489590940481, 0.109848283385112, 0.11358904634185, 0.128425331060705,
0.136830729164909), b_fi.frail = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1), pct = c(47.5830407777478, 52.4169592222522, 37.567084078712,
62.432915921288, 36.9897959183673, 63.0102040816327, 34.0960360941025,
65.9039639058975, 29.0891283055828, 70.9108716944172)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(
q = 1:5, .rows = structure(list(1:2, 3:4, 5:6, 7:8, 9:10), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), .drop = TRUE))
My attempt:
ggplot(dat, aes(x=mean_exp, y=mean_fi, linetype=is_female)) +
geom_point(aes(color=is_female, fill=is_female), size=2, shape="square") +
geom_line() +
scale_x_continuous(limits = c(18, 30), breaks = seq(20, 30, 5), expand = c(0,0)) +
scale_y_continuous(limits = c(0.05, 0.15), breaks = seq(0.05, 0.15, 0.02), expand = c(0,0)) +
scale_color_manual(values = c("M" = "black", "F" = "black")) +
scale_fill_manual(values = c("M" = "black", "F" = "white")) +
labs(x= expression(Body ~ mass ~ index ~ (kg/m^2)), y= "Mean baseline FI score", title = "BMI") +
theme_classic() +
theme(plot.title = element_text(hjust=0.5),
legend.position = "bottom")
My code outputs a graph with grey points and lines for some reason. I would like black lines and outlines (but points to be filled with black or white depending on the group) and to combine the legend if possible.
I realise there are many similar examples out there but I can't seem to figure out why my code is not working... Thanks for your help in advance!
Use shape = 22 for a fillable square, i.e. one which can have a different outline.
Colour can be outside the aes if both groups require a black outline for the points.
library(dplyr)
library(ggplot2)
dat <- structure(list(
q = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L),
is_female = structure(c(
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L
), levels = c("Male", "Female"), class = "factor"), mean_exp = c(
19.3095475534936,
19.2351713991988, 21.6718804471611, 21.69517120871, 23.4144749290445,
23.4191383190372, 25.2817658487443, 25.2772340355605, 28.3982168611512,
28.7869521340185
), mean_fi = c(
0.0915386254018914, 0.0912295567094683,
0.0771055282779973, 0.0790597510143077, 0.0859508568981647,
0.088489590940481, 0.109848283385112, 0.11358904634185, 0.128425331060705,
0.136830729164909
), b_fi.frail = c(
1, 1, 1, 1, 1, 1, 1, 1,
1, 1
), pct = c(
47.5830407777478, 52.4169592222522, 37.567084078712,
62.432915921288, 36.9897959183673, 63.0102040816327, 34.0960360941025,
65.9039639058975, 29.0891283055828, 70.9108716944172
)
), class = c(
"grouped_df",
"tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), groups = structure(list(
q = 1:5, .rows = structure(list(1:2, 3:4, 5:6, 7:8, 9:10), ptype = integer(0), class = c(
"vctrs_list_of",
"vctrs_vctr", "list"
))
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L), .drop = TRUE))
ggplot(dat, aes(x = mean_exp, y = mean_fi, linetype = is_female)) +
geom_point(aes(fill = is_female), size = 2, shape = 22, colour = "black") +
geom_line() +
scale_x_continuous(limits = c(18, 30), breaks = seq(20, 30, 5), expand = c(0, 0)) +
scale_y_continuous(limits = c(0.05, 0.15), breaks = seq(0.05, 0.15, 0.02), expand = c(0, 0)) +
scale_fill_manual(values = c("black", "white")) +
labs(x = expression(Body ~ mass ~ index ~ (kg / m^2)), y = "Mean baseline FI score",
title = "BMI", fill = "Sex", linetype = "Sex") +
theme_classic() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "bottom"
)
Created on 2022-07-07 by the reprex package (v2.0.1)
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)
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!
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
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()