Age pyramid in R_issues

Hi,
covid_age.pdf (47.4 KB)
I am trying to draw age pyramid in R. But not quite successful. Please see below the reprex. Error message is saying that the object "covid_age" is not found, but I have uploaded the data correctly. Please see the attached data.

I appreciate your help.

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.0.2
library(ggpol)
#> Warning: package 'ggpol' was built under R version 4.0.2
library(ggplot2)

co1<-ggplot(data=covid_age, aes(x = age, y =cfr, fill =sex)) +
  geom_bar(stat = "identity") +
  facet_share(~sex, dir = "h", scales = "free", reverse_num = TRUE) +   # note: scales = "free"
  coord_flip() +
  theme_minimal() +
  labs(y = "Count", x = "Age Band", title = " ") +
  scale_fill_manual(values = c("pink", "blue"))
#> Error in ggplot(data = covid_age, aes(x = age, y = all_cases, fill = sex)): object 'covid_age' not found
co1
#> Error in eval(expr, envir, enclos): object 'co1' not found

Assuming you have shared all the code you have run, then I can say that you have not loaded the data in a format R can understand. What format is the data in? Is it a CSV, XLSX, or the PDF you have uploaded?

Yes, i uploaded the pdf data

How (with what code) did you convert the PDF data to the required data.frame format in your workspace?

I imported data file that was in excel spread sheet. Since I couldn't upload the excel file, i uploaded the data table in PDF.

Sorry for my clumsy way of formulating. I will reformulate:

You have a problem with plotting your data.
You say you have imported your data (from a spreadsheet I understand now).
To help you pinpoint the problem it would be helpful if you show us the first rows of the imported data e.g. by using str(covid_age)

Thank you for the quick response. I used following command to import the data to R environment.

library(readxl)
covid_age <- read_excel("Desktop/covid_age.xlsx")

I think that is in your interest to show the result of str(covid_age) . It is polite to give readers that invest some time in your problem the information you and they think is relevant.
The standard procedure is to provide a reprex .

Looking for a solution I saw on the repository of the ggpol package that the function facet_share is currently not supported: issue7

For another way to create a pyramid see https://forum.posit.co/t/adding-legend-to-ggplot/73644

Thank you Han for trying. i appreciate it.

Dear Han,

I try to run exactly the same script you sent. but the age-groups on the Y-axsis was not aligned in descending order.

it seems your age bands are not factors with levels in the correct order.
What code did you use to make them ?

I used the following code
library(ggplot2)

pyramida2010 <- data.frame(
value = c(-289132,-242402,-235471,-315905,-361233,
-393253,-476520,-435327,-359838,-345961,-349514,-370932,
-341085,-243367,-155444,-124365,-77384,-33230,-5305,
-1362,275413,228558,223394,300086,339507,365668,450984,
410637,341023,333977,351237,390758,380115,293910,212293,
196457,149630,84217,16110,5079),
vek = as.factor(c("0-4","5-9","10-14",
"15-19","20-24","25-29","30-34","35-39","40-44",
"45-49","50-54","55-59","60-64","65-69","70-74",
"75-79","80-84","85-89","90-94","95-99","0-4",
"5-9","10-14","15-19","20-24","25-29","30-34",
"35-39","40-44","45-49","50-54","55-59","60-64",
"65-69","70-74","75-79","80-84","85-89","90-94",
"95-99")),
variable = as.factor(c("muži","muži","muži",
"muži","muži","muži","muži","muži","muži",
"muži","muži","muži","muži","muži","muži","muži",
"muži","muži","muži","muži","ženy","ženy","ženy",
"ženy","ženy","ženy","ženy","ženy","ženy",
"ženy","ženy","ženy","ženy","ženy","ženy","ženy",
"ženy","ženy","ženy","ženy"))
)

cols <- c("muži"= "#254061", "ženy" = "#CD3C33")

ggplot(pyramida2010, aes(y = vek, x = value, fill = variable)) +
geom_bar(data=subset(pyramida2010,variable=="ženy"), stat = "identity") +
geom_bar(data=subset(pyramida2010,variable=="muži"), stat = "identity") +
theme(axis.text.x=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())+
labs(title = "Věková pyramida 2010",
fill = "")+
scale_colour_manual(values=cols)

Edited to use forcats::as_factor which sets the levels based on order encountered (rather than alphabetic)

library(ggplot2)
library(forcats)
pyramida2010 <- data.frame(
  value = c(-289132,-242402,-235471,-315905,-361233,
            -393253,-476520,-435327,-359838,-345961,-349514,-370932,
            -341085,-243367,-155444,-124365,-77384,-33230,-5305,
            -1362,275413,228558,223394,300086,339507,365668,450984,
            410637,341023,333977,351237,390758,380115,293910,212293,
            196457,149630,84217,16110,5079),
  vek = as_factor(c("0-4","5-9","10-14",
                    "15-19","20-24","25-29","30-34","35-39","40-44",
                    "45-49","50-54","55-59","60-64","65-69","70-74",
                    "75-79","80-84","85-89","90-94","95-99","0-4",
                    "5-9","10-14","15-19","20-24","25-29","30-34",
                    "35-39","40-44","45-49","50-54","55-59","60-64",
                    "65-69","70-74","75-79","80-84","85-89","90-94",
                    "95-99")),
  variable = as.factor(c("muži","muži","muži",
                         "muži","muži","muži","muži","muži","muži",
                         "muži","muži","muži","muži","muži","muži","muži",
                         "muži","muži","muži","muži","ženy","ženy","ženy",
                         "ženy","ženy","ženy","ženy","ženy","ženy",
                         "ženy","ženy","ženy","ženy","ženy","ženy","ženy",
                         "ženy","ženy","ženy","ženy"))
)

cols <- c("muži"= "#254061", "ženy" = "#CD3C33")

ggplot(pyramida2010, aes(y = vek, x = value, fill = variable)) +
  geom_bar(data=subset(pyramida2010,variable=="ženy"), stat = "identity") +
  geom_bar(data=subset(pyramida2010,variable=="muži"), stat = "identity") +
  theme(axis.text.x=element_blank(),
        axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  labs(title = "Věková pyramida 2010",
       fill = "")+
  scale_colour_manual(values=cols)

Thank you very much. I was able to use the code and develop age pyramid. I appreciate this timely support.

Once again thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.