Good morning everyone, I have this issue while using ggplot and creating a bar chart with two axes. I've managed to get the bars as I wanted, but I can't seem to change the axis labels on the x-axis. Every two bars correspond to "Resistant" and "Susceptible." Then, the first 12 bars are for 48 hpi, and the last 4 are for 72 hpi. Lastly, the first 4 columns are for "Blood," the next 4 for "Lung," and the last 8 correspond to "Nasal Turbinate." I would like to label these on the x-axis, but I can't figure it out."
DF_blood <- data.frame(Group = c("Resistant", "Susceptible"),
Host_DEGs = c(3558, 5635),
Viral_counts = c(0.4771213, 5.608774))
DF_Lung <- data.frame(Group = c("Resistant", "Susceptible"),
Host_DEGs = c(126, 10866),
Viral_counts = c( 1.531479, 6.325484))
DF_NasalT_72 <- data.frame(Group = c("Resistant", "Susceptible"),
Host_DEGs = c(2830, 4632),
Viral_counts = c(0, 4.716613))
DF_NasalT_48 <- data.frame(Group = c("Resistant", "Susceptible"),
Host_DEGs = c(81, 6126),
Viral_counts = c(2.41162, 4.985206))
# Combina los dataframes DF de sangre, pulmón y turbinate nasal en uno solo
combined_df <- rbind(DF_blood, DF_Lung, DF_NasalT_48, DF_NasalT_72)
DFlong <- combined_df |> pivot_longer(cols = -Group, names_to = "Type") |>
mutate(scaled_value = ifelse(Type == "Host_DEGs", value, value / 0.001))
combined_df$Sample <- c("Blood", "Blood", "Blood", "Blood", "Lung", "Lung", "Lung", "Lung", "Nasal_Turbinate","Nasal_Turbinate", "Nasal_Turbinate","Nasal_Turbinate", "Nasal_Turbinate", "Nasal_Turbinate","Nasal_Turbinate")
combined_df$Timepoint <- c("48hpi","48hpi","48hpi","48hpi","48hpi","48hpi","72hpi","72hpi")
DFlong <- DF |> pivot_longer(cols = -Group, names_to = "Type") |>
mutate(scaled_value = ifelse(Type == "Host_DEGs", value, value / 0.001))
DFlong$Sample <- c("Blood", "Blood", "Blood", "Blood", "Lung", "Lung", "Lung", "Lung", "Nasal_Turbinate", "Nasal_Turbinate", "Nasal_Turbinate", "Nasal_Turbinate", "Nasal_Turbinate", "Nasal_Turbinate", "Nasal_Turbinate","Nasal_Turbinate")
DFlong$Timepoint <- c("48hpi","48hpi","48hpi","48hpi","48hpi","48hpi","48hpi","48hpi","48hpi","48hpi","48hpi","48hpi","72hpi","72hpi","72hpi","72hpi")
ggplot(DFlong, aes(x = interaction(Group, Timepoint, Sample), y = scaled_value, fill = Type)) +
geom_col(position = "dodge") +
scale_y_continuous(name = "Host DEGs", limits = c(0, 11000), sec.axis = sec_axis(~ . * 0.0009, name = "Viral gene counts (log10)")) +
labs(y = "Host DEGs") +
theme_minimal() +
theme(text = element_text(size = 20)) +
coord_cartesian(ylim = c(0, 7500)) +
annotate("text", x = 1:4, y = - 400,
label = rep(c("Resistant", "Susceptible"), 2)) +
annotate("text", c(1.5, 3.5), y = - 800, label = c("48 hpi", "48 hpi"))
sessionInfo( )