Determining the Scale factor for the secondary Y-axis - r

The dput(Q_Sheet) is below. How can properly introduce a second y-axis that is different in scale from the primary axis?
structure(list(Amino_acids = c(4, 12, 20, 28, 32), Protein_length_Ang = c(7,
24, 40, 56, 64), length_no_ratio = c(1.75, 2, 2, 2, 2), Corrected_aa = c(1.24459201924769e-12,
3.71007650662474e-09, 1.10594599229843e-05, 0.0319159404863842,
0.642857142857143), aa_frequency = c(3.99735380592756, 6.96840672963299,
4.58228895300999, 3.12310921028256, 4.67560843680985), T_degC = c(50.3857804818545,
52.8464583426248, 60.0760389538482, 58.1895053328481, 67.628202708438
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
), na.action = structure(c(`2` = 2L, `4` = 4L, `6` = 6L), class = "omit"))
`
ggplot(data = Q_Sheet, aes(x = T_degC))+
geom_line(aes(y = Amino_acids), color="red")+
geom_line(aes(y = Corrected_aa), color = "blue") +
scale_y_continuous(name = "Amino_acids", sec.axis = sec_axis(~.*10, name = "Corrected_aa"))
The output is as follows:
<ScaleContinuousPosition>
Range:
Limits: 0 -- 1

You can use the below formula to keep the secondary Y-axis at the same level as Corrected_aa.
library(ggplot2)
ggplot(data=Q_Sheet, aes(x=T_degC))+
geom_line(aes(y=Amino_acids),color="red")+
geom_line(aes(y=Corrected_aa),color="blue")+
scale_y_continuous(name="Amino_acids",
sec.axis=sec_axis(~{
a <- min(Q_Sheet$Corrected_aa)
b <- max(Q_Sheet$Corrected_aa)
((((b-a) * (. - min(.)))/diff(range(.))) + a)
},name="Corrected_aa"))

There are two issues - 1) scale_y_continuous typo and 2) there is a missing + connecting the last expression
ggplot(data=Q_Sheet, aes(x=T_degC))+
geom_line(aes(y=Amino_acids),color="red")+
geom_line(aes(y=Corrected_aa),color="blue") +
scale_y_continuous(name="Amino_acids",
sec.axis=sec_axis(~.*10,name="Corrected_aa"))
-ouptut

We could define a coefficient and then color the lines to indicate wich lines belongs to which y-scale:
library(ggplot2)
value used to transform the data
coeff <- 0.01
# colors
Amino_acidsColor = "red"
Corrected_aaColor = "blue"
ggplot(data=Q_Sheet, aes(x=T_degC))+
geom_line(aes(y=Amino_acids), size = 2, color=Amino_acidsColor)+
geom_line(aes(y=Corrected_aa/coeff), size = 2, color=Corrected_aaColor) +
scale_y_continuous(name="Amino_acids",
sec.axis=sec_axis(~.*coeff,name="Corrected_aa")) +
theme_bw() +
theme(
axis.title.y = element_text(color = Amino_acidsColor, size=13),
axis.title.y.right = element_text(color = Corrected_aaColor, size=13)
)

Related

Add geom_text to waffle chart with ggplot2

With the following code, I used waffle package to generate a waffle chart
library(tidyverse)
library(waffle)
df <- structure(list(
parts = c("case_1", "case_2", "case_3"),
values = c(11, 34, 55)
),
row.names = c(NA, -3L),
class = c("tbl_df", "tbl", "data.frame")
)
df
p <- df %>% ggplot() +
geom_pictogram(
n_rows = 10, aes(label = parts, values = values, color = parts),
family = "FontAwesome5Free-Solid",
flip = TRUE,
size = 10
) +
scale_label_pictogram(
name = "Cases", values = c("male"),
) +
scale_color_manual(
name = "Cases",
values = c("case_1" = "red", "case_2" = "green", "case_3" = "grey85"
)
) +
coord_equal() +
theme_minimal() +
theme(legend.position = "bottom")
p
Then, I tried to add "45%" to the chart with geom_text. However, it did not show "45%" but the icon instead like this:
p + geom_text(aes(x = 5, y = 7, label = "45%"),
size = 24,
show.legend = FALSE
)

Plotting kaplan meier survival data if dataset already contains time , survival probability, and upper/lower 95% confidence intervals

I have a kaplan meier survival dataset that already contains time, survival probabilities values, and survival probability data points for both the lower & upper 95% CI. I have posted a clip of my dataset below. I was hoping if anyone knew how merge my two plots, normalize them, and make my plots continuous despite missing values. I was hoping for my final graph to look like this 2.
kmcurvetest_2[1:20, ] %>% dput()
structure(list(Time = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 15, 16, 17, 18, 19, 20), Cohort1 = c(0.904255319148936,
0.898936170212766, 0.887769261266023, 0.887769261266023, 0.887769261266023,
0.87631417402388, 0.87631417402388, NA, NA, 0.87631417402388,
0.864551567661143, 0.858629981581273, 0.852708395501402, NA,
0.852708395501402, 0.846745399728665, 0.846745399728665, 0.840740113205766,
NA, 0.840740113205766), C1Lower95 = c(0.852338104650895, 0.846140749965675,
0.833054851312184, 0.833054851312184, 0.833054851312184, 0.819696863257612,
0.819696863257612, NA, NA, 0.819696863257612, 0.806043967960357,
0.799218079053227, 0.792429563598159, NA, 0.792429563598159,
0.785616930383783, 0.785616930383783, 0.778778500012501, NA,
0.778778500012501), C1Upper95 = c(0.938570469008423, 0.934312293965728,
0.92534844712446, 0.92534844712446, 0.92534844712446, 0.916056348120451,
0.916056348120451, NA, NA, 0.916056348120451, 0.906427391600421,
0.901537491012523, 0.8966168920045, NA, 0.8966168920045, 0.891638921203334,
0.891638921203334, 0.886603579837755, NA, 0.886603579837755),
Cohort2 = c(0.707462686567164, 0.692537313432835, 0.683384837924912,
0.674232362416989, 0.674232362416989, 0.668074989244231,
NA, 0.664996302657852, 0.664996302657852, 0.658781383941424,
0.652507275522934, 0.649370221313689, 0.646217938685953,
0.643065656058216, 0.630394411603867, 0.62722660049028, 0.624058789376693,
0.620890978263105, 0.617723167149518, 0.614539027112665),
C2Lower95 = c(0.655564487332025, 0.640091667602195, 0.630607727619003,
0.62114710952213, 0.62114710952213, 0.614788099004335, NA,
0.611612499799214, 0.611612499799214, 0.605202384226936,
0.598734349944198, 0.595504428845739, 0.592259587632446,
0.589017489398546, 0.576004700295779, 0.572758317180272,
0.569514623188025, 0.566273601091399, 0.56303523423295, 0.5597807789553
), C2Upper95 = c(0.753046097156017, 0.738936670959587, 0.730275198102735,
0.721591223004285, 0.721591223004285, 0.715742377703966,
NA, 0.712814219355565, 0.712814219355565, 0.706901638437748,
0.700928732359048, 0.697938428282602, 0.694932646561064,
0.691924293962202, 0.679812432812405, 0.67677809121533, 0.673741229385084,
0.670701861632804, 0.667660001811057, 0.664601682804447)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
My data set contains missing values and I attempted to make my geom_line continuous despite the missing values using ggplot(data = kmcurvetest_2[!is.na(kmcurvetest_2$Cohort2),] , mapping = aes(x = Time, y = Cohort2)
My codes for the two plots are...
# plot cohort 1
ggplot(data = kmcurvetest_2[!is.na(kmcurvetest_2$Cohort1),] , mapping = aes(x = Time, y = Cohort1)) +
geom_point(size = 1 ) +
geom_line(color = "blue") +
geom_ribbon(aes(x = Time, ymin = C1Lower95, ymax = C1Upper95),
fill = "blue", alpha = 0.2) +
labs(title = paste("Inpatient Hospitalization"), x = "Time [Days]", y = "Survival [%]") +
scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
scale_color_discrete(name = "Cohort", labels = c("Cohort1"))
# plot cohort 2
ggplot(data = kmcurvetest_2[!is.na(kmcurvetest_2$Cohort2),] , mapping = aes(x = Time, y = Cohort2)) +
geom_point(size = 1 ) +
geom_line(color = "red") +
geom_ribbon(aes(x = Time, ymin = C2Lower95, ymax = C2Upper95),
fill = "red", alpha = 0.2) +
labs(title = paste("Inpatient Hospitalization"), x = "Time [Days]", y = "Survival [%]") +
scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
scale_color_discrete(name = "Cohort", labels = c("Cohort1"))
Thank you I really appreciate it - I have attached the images in question for reference above!
Something like this?
library(tidyverse)
df1 <- df %>%
slice(1) %>%
mutate(across(-time, ~paste(1))) %>%
type.convert(as.is = TRUE) %>%
bind_rows(df %>% mutate(time = time+1))
ggplot(df1, aes(x=factor(time), group=1)) +
geom_line(data = df1 %>% dplyr::select(1:4), aes(y=C1survival, color = "red"), size=1)+
geom_point(data = df1 %>% dplyr::select(1:4), aes(y=C1survival), shape = 3, color = "black")+
geom_ribbon(data = df1 %>% dplyr::select(1:4), aes(ymin = C1lower95.CI, ymax = C1upper95.CI), alpha = 0.2)+
labs(title = paste("Survival cohort1"), x = "Time [days]", y = "Survival [%]") +
geom_line(data = df1 %>% dplyr::select(1, 5:7), aes(y=C2survival, color = "blue"), size=1)+
geom_point(data = df1 %>% dplyr::select(1:5:7), aes(y=C2survival), shape = 3, color = "black")+
geom_ribbon(data = df1 %>% dplyr::select(1, 5:7), aes(ymin = C2lower95.CI, ymax = C2upper95.CI), alpha = 0.2)+
scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
scale_color_discrete(name = "Cohort", labels = c("Cohort1", "Cohort2"))+
theme_classic()+
theme(
axis.line = element_line(colour = "black", size = 0.24),
aspect.ratio = 4 / 5,
legend.position = "bottom",
legend.box = "horizontal")
data:
structure(list(time = 0:8, C1survival = c(0.904255319, 0.89893617,
0.887769261, 0.887769261, 0.887769261, 0.876314174, 0.876314174,
0.664996303, 0.664996303), C1lower95.CI = c(0.852338105, 0.84614075,
0.833054851, 0.833054851, 0.833054851, 0.819696863, 0.819696863,
0.6116125, 0.6116125), C1upper95.CI = c(0.938570469, 0.934312294,
0.925348447, 0.925348447, 0.925348447, 0.916056348, 0.916056348,
0.712814219, 0.712814219), C2survival = c(0.707462687, 0.692537313,
0.683384838, 0.674232362, 0.674232362, 0.668074989, NA, NA, NA
), C2lower95.CI = c(0.655564487, 0.640091668, 0.630607728, 0.62114711,
0.62114711, 0.614788099, NA, NA, NA), C2upper95.CI = c(0.753046097,
0.738936671, 0.730275198, 0.721591223, 0.721591223, 0.715742378,
NA, NA, NA)), class = "data.frame", row.names = c(NA, -9L))

Only one of two numeric columns is displaying in ggplot2 bar chart using R

I have a dataset with three columns. One is Player; the second is Run_Value; the third is Num_years. I want Player to appear on the Y axis, Run_Value as a bar on the X axis, and Num_Years to appear either as a smaller bar within the Run_Value bar or next to it. Below is how my bar chart is currently appearing. The Num_Years data is not displaying, which is what I need.
In addition, if at the end of each Run_Value bar its Run_Value could appear, that would be great.
I have searched Stackoverflow for a comparable question, but could not find one. If one exists, please share it link.
--I cannot see the two uploaded images on my screen and am not sure why.
Dplyr Code:
sp2358test <- spAll |>
filter(RunValue < 0) |>
select(Player, RunValue) |>
add_count(Player, name="Num_years") |>
filter(Num_years %in% c(2:8)) |>
arrange(Num_years, Player) |>
group_by(Player, Num_years) |>
summarise(Run_Value = sum(RunValue))
Partial dplyr results
My ggplot2 code
sp2358test |>
ggplot(aes(fill=Run_Value, y=Player, x=Run_Value)) +
geom_bar(position='dodge', stat='identity') +
lims(x = c(-250,0)) +
guides(fill=guide_legend(title="Legend")) +
ggtitle("Mets Starters with Top Run Value Totals",
subtitle = "Data from Statcast for 2008-2021") +
theme(plot.title = element_text(size = 14, color = "black"),
plot.subtitle = element_text(size = 11, color = "purple"),
text=element_text(color="blue"),
axis.text=element_text(color="black"),
legend.title = element_text(color = "black", size = 11)
)
My ggplot2 image
Results of dput for spAll dataframe:
structure(list(Player = c("deGrom, Jacob", "Dickey, R.A.", "Gee, Dillon",
"Harvey, Matt", "Lugo, Seth", "Maine, John", "Matz, Steven",
"Niese, Jonathon", "Pelfrey, Mike", "Santana, Johan", "Syndergaard, Noah",
"Wheeler, Zack"), Num_years = c(8L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
3L, 3L, 5L, 3L), Run_Value = c(-240.4, -56.3, -11.2, -70.3, -8.5,
-8.1, -16, -11.8, -20.7, -87.8, -77.5, -43.1)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), groups = structure(list(
Player = c("deGrom, Jacob", "Dickey, R.A.", "Gee, Dillon",
"Harvey, Matt", "Lugo, Seth", "Maine, John", "Matz, Steven",
"Niese, Jonathon", "Pelfrey, Mike", "Santana, Johan", "Syndergaard, Noah",
"Wheeler, Zack"), .rows = structure(list(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), .drop = TRUE))
You may want to avoid having two x-axis. Consider the alternative of using facet_wrap. Note you first need to put your data into a long format. I've used df for your dataset.
library(ggplot2)
library(dplyr)
library(tidyr)
df2 <- df %>%
pivot_longer(cols = c(Num_years, Run_Value), names_to = "metric")
df2 %>%
ggplot() +
geom_col(aes(y=Player, x = value, fill = value)) +
facet_wrap(~rev(metric),scales = "free_x") +
ggtitle("Mets Starters with Top Run Value Totals",
subtitle = "Data from Statcast for 2008-2021") +
theme(plot.title = element_text(size = 14, color = "black"),
plot.subtitle = element_text(size = 11, color = "purple"),
text=element_text(color="blue"),
axis.text=element_text(color="black"),
legend.title = element_text(color = "black", size = 11)
)
It gets you to something that looks like this. The problem is that you have two variables that are using the fill scale and those two variables have very different values. What is your intention?
Here's another that is closer to your original and what you describe as you want.
library(ggplot2)
library(dplyr)
library(tidyr)
df2 <- df %>%
pivot_longer(cols = c(Num_years, Run_Value), names_to = "metric")
df2 %>%
ggplot() +
geom_bar(aes(y=Player, x = value, group = metric, fill = metric), stat='identity',
position = "dodge") +
ggtitle("Mets Starters with Top Run Value Totals",
subtitle = "Data from Statcast for 2008-2021") +
theme(plot.title = element_text(size = 14, color = "black"),
plot.subtitle = element_text(size = 11, color = "purple"),
text=element_text(color="blue"),
axis.text=element_text(color="black"),
legend.title = element_text(color = "black", size = 11)
)
You have positive numbers in one variable and negative in the other. It was important to omit the xlim otherwise it would remove all the positive values of the Num-years variable.
I think this is what you are looking for. I reversed the sign of Run_Value so that the data can be plotted as positive values. I sorted the y-axis by Years. I used colors to differentiate the two x-axis. The secondary axis has to be scaled to the first so I divided by 10. That is why it goes to 25 and the primary goes to 250. To align, Years is scaled by multiplying by 10.
df %>%
mutate(Run_Value = -(Run_Value)) %>%
ggplot() +
geom_bar(aes(x = Run_Value, y = reorder(Player, Num_years), fill = "Run Value"), stat = 'identity') +
geom_bar(aes(x = (Num_years * 10), y = Player, fill = "Years"), stat = 'identity', width = 0.3) +
geom_text(aes(x = (Num_years * 10), y = Player, label = Num_years), color = "white", hjust = 1) +
ggtitle("Mets Starters with Top Run Value Totals",
subtitle = "Data from Statcast for 2008-2021") +
scale_x_continuous(name = "Negative Run Value", sec.axis = sec_axis(~ ./10, name = "Years")) +
scale_fill_manual(name = "Legend", values = c("orange" ,"blue")) +
theme(plot.title = element_text(size = 14, color = "black"),
plot.subtitle = element_text(size = 11, color = "purple"),
text=element_text(color="black"),
axis.text=element_text(color="black"),
legend.title = element_text(color = "black", size = 11),
axis.title.x = element_text(color = "orange"),
axis.title.x.top = element_text(color = "blue")) +
labs( y = "Player (Ordered by Years)")

Positioning labels and color coding in sunburst - R

This is what is the output.I have a data set which contains unit, weight of each unit and compliance score for each unit in year 2016.
I was not able to add the table but here is the screenshot for the data in csv
I have named the columns in the data as unit, weight and year(which is compliance score) .
I want to create a sunburst chart where the first ring will be the unit divided based on weight and the second ring will be the same but will have labels compliance score.
The colour for each ring will be different.
I was able to do some code with the help from an online blog and the output I have gotten is similar to what I want but I am facing difficulty in positioning of the labels and also the colour coding for each ring
#using ggplot
library(ggplot2) # Visualisation
library(dplyr) # data wrangling
library(scales) # formatting
#read file
weight.eg = read.csv("Dummy Data.csv", header = FALSE, sep =
";",encoding = "UTF-8")
#change column names
colnames(weight.eg) <- c ("unit","weight","year")
#as weight column is factor change into integer
weight.eg$weight = as.numeric(levels(weight.eg$weight))
[as.integer(weight.eg$weight)]
weight.eg$year = as.numeric(levels(weight.eg$year))
[as.integer(weight.eg$year)]
#Nas are introduced, remove
weight.eg <- na.omit(weight.eg)
#Sum of the total weight
sum_total_weight = sum(weight.eg$weight)
#First layer
firstLevel = weight.eg %>% summarize(total_weight=sum(weight))
sunburst_0 = ggplot(firstLevel) # Just a foundation
#this will generate a bar chart
sunburst_1 =
sunburst_0 +
geom_bar(data=firstLevel, aes(x=1, y=total_weight),
fill='darkgrey', stat='identity') +
geom_text(aes(x=1, y=sum_total_weight/2, label=paste("Total
Weight", comma(total_weight))), color='black')
#View
sunburst_1
#this argument is used to rotate the plot around the y-axis which
the total weight
sunburst_1 + coord_polar(theta = "y")
sunburst_2=
sunburst_1 +
geom_bar(data=weight.eg,
aes(x=2, y=weight.eg$weight, fill=weight.eg$weight),
color='white', position='stack', stat='identity', size=0.6)
+
geom_text(data=weight.eg, aes(label=paste(weight.eg$unit,
weight.eg$weight), x=2, y=weight.eg$weight), position='stack')
sunburst_2 + coord_polar(theta = "y")
sunburst_3 =
sunburst_2 +
geom_bar(data=weight.eg,
aes(x=3, y=weight.eg$weight,fill=weight.eg$weight),
color='white', position='stack', stat='identity',
size=0.6)+
geom_text(data = weight.eg,
aes(label=paste(weight.eg$year),x=3,y=weight.eg$weight),position =
'stack')
sunburst_3 + coord_polar(theta = "y")
sunburst_3 + scale_y_continuous(labels=comma) +
scale_fill_continuous(low='white', high='darkred') +
coord_polar('y') + theme_minimal()
Output for dput(weight.eg)
structure(list(unit = structure(2:7, .Label = c("", "A", "B",
"C", "D", "E", "F", "Unit"), class = "factor"), weight = c(30,
25, 10, 17, 5, 13), year = c(70, 80, 50, 30, 60, 40)), .Names =
c("unit",
"weight", "year"), row.names = 2:7, class = "data.frame", na.action
= structure(c(1L,
8L), .Names = c("1", "8"), class = "omit"))
output for dput(firstLevel)
structure(list(total_weight = 100), .Names = "total_weight", row.names
= c(NA,
-1L), na.action = structure(c(1L, 8L), .Names = c("1", "8"), class =
"omit"), class = "data.frame")
So I think I might have some sort of solution for you. I wasn't sure what you wanted to color-code on the outer ring; from your code it seems you wanted it to be the weight again, but it was not obvious to me. For different colour scales per ring, you could use the ggnewscale package:
library(ggnewscale)
For the centering of the labels you could write a function:
cs_fun <- function(x){(cumsum(x) + c(0, cumsum(head(x , -1))))/ 2}
Now the plotting code could look something like this:
ggplot(weight.eg) +
# Note: geom_col is equivalent to geom_bar(stat = "identity")
geom_col(data = firstLevel,
aes(x = 1, y = total_weight)) +
geom_text(data = firstLevel,
aes(x = 1, y = total_weight / 2,
label = paste("Total Weight:", total_weight)),
colour = "black") +
geom_col(aes(x = 2,
y = weight, fill = weight),
colour = "white", size = 0.6) +
scale_fill_gradient(name = "Weight",
low = "white", high = "darkred") +
# Open up new fill scale for next ring
new_scale_fill() +
geom_text(aes(x = 2, y = cs_fun(weight),
label = paste(unit, weight))) +
geom_col(aes(x = 3, y = weight, fill = weight),
size = 0.6, colour = "white") +
scale_fill_gradient(name = "Another Weight?",
low = "forestgreen", high = "white") +
geom_text(aes(label = paste0(year), x = 3,
y = cs_fun(weight))) +
coord_polar(theta = "y")
Which looks like this:

Sunburst plot - Error in generating more than 2 rings

I have got this data set and want to generate a sunburst plot. The data is of 4 columns which are unit, weight, year16 and year17. The sunburst is based on the values in the weight column. The code is there and when adding the coding for the third layer it is giving me an error. I think the error is coming when I am adding the third layer.
library("ggnewscale")
library(ggplot2)
#read file
weight.eg = read.csv("Dummy Data.csv", header = FALSE, sep =
";",encoding = "UTF-8")
#change column names
colnames(weight.eg) <- c
("unit","weight","year16","year17")
#check the class
sapply(weight.eg, class)
#View(weight.eg)
#as weight column is factor change into integer
weight.eg$weight = as.numeric(levels(weight.eg$weight))
[as.integer(weight.eg$weight)]
weight.eg$year16 = as.numeric(levels(weight.eg$year16))
[as.integer(weight.eg$year16)]
weight.eg$year17 = as.numeric(levels(weight.eg$year17))
[as.integer(weight.eg$year17)]
#Nas are introduced, remove
weight.eg <- na.omit(weight.eg)
#Sum of the total weight
sum_total_weight = sum(weight.eg$weight)
#First layer
firstLevel = weight.eg %>% summarize(total_weight=sum(weight))
cs_fun <- function(x){(cumsum(x) + c(0, cumsum(head(x , -1))))/ 2}
ggplot(weight.eg) +
geom_col(data = firstLevel,
aes(x = 1, y = total_weight)) +
geom_text(data = firstLevel,
aes(x = 1, y = total_weight / 2,
label = paste("Total Weight:", total_weight)),
colour = "black") +
geom_col(aes(x = 2,
y = weight, fill = weight),
colour = "black", size = 0.6) +
scale_fill_gradient(name = "Weight",
low = "white", high = "lightblue") +
# Open up new fill scale for next ring
new_scale_fill() +
geom_text(aes(x = 2, y = cs_fun(weight),
label = paste(unit, weight))) +
geom_col(aes(x = 3, y = weight, fill = year16),
size = 0.6, colour = "black") +
scale_fill_gradient(name = "Year16",
low = "red", high = "green") +
geom_text(aes(label = paste0(unit,year16), x = 3,
y = cs_fun(weight))) +
#next ring
new_scale_fill() +
geom_text(aes(x = 2, y = cs_fun(weight),
label = paste(unit, weight))) +
geom_col(aes(x = 4, y = weight, fill = year17),
size = 0.6, colour = "black") +
scale_fill_gradient(name = "Year17",
low = "red", high = "green") +
geom_text(aes(label = paste0(unit,year17), x = 4,
y = cs_fun(weight))) +
coord_polar(theta = "y")
The output for dput(weight.eg) is
structure(list(unit = structure(1:6, .Label = c("A", "B", "C",
"D", "E", "F", "Unit"), class = "factor"), weight = c(30, 25,
10, 17, 5, 13), year16 = c(70, 80, 50, 30, 60, 40), year17 = c(50,
100, 20, 30, 70, 60)), .Names = c("unit", "weight", "year16",
"year17"), row.names = 2:7, class = "data.frame", na.action =
structure(1L, .Names = "1", class = "omit"))
I want to include year17 as well and in the future there will be
columns, so that has to be added as well. Because of the error I
am not able to figure out what is wrong.

Resources