简体   繁体   中英

Reordering Plot and Changing Axis Size (R ggplot)

I'm trying to change in both of my plots, the order and the x axis size for both. These are not being able to be changed accordingly

DF Creation

contig_count_average <- data.frame(Genome_Type = c("MBT", "Anglucyclines", "Whole Genome"),
                                   Contig_Count_Average = c("2.91","83.7","608.3"))

Plot

p2 <- ggplot(contig_count_average, mapping = aes(x = reorder(Genome_Type, Contig_Count_Average), Contig_Count_Average, fill = Genome_Type))  + 
  xlab("Genome") +
  ylab("Contig No.") + 
  ggtitle("Contig Count per Genome Distribution") +
  geom_bar(stat = "identity") +
  theme(text = element_text(size=20),
       axis.text.x = element_text(angle=90, hjust=1)) +
  guides(fill=guide_legend(title="Genome Type")) +
  coord_flip() +
  theme_bw() +
  scale_y_continuous(limits = c(0,2835), expand = c(0, 0)) +
  scale_x_discrete(labels = abbreviate)
p

I get the following warning:

1: In Ops.factor(Contig_Count_Average) : ‘-’ not meaningful for factors

The issue is because Contig_Count_Average is treated as factors in contig_count_average .

We can change it to numeric by doing either:

contig_count_average <- type.convert(contig_count_average, as.is = TRUE

Or

contig_count_average$Contig_Count_Average <- as.numeric(as.character(contig_count_average$Contig_Count_Average))

and then use the ggplot code.

p2 <- ggplot(contig_count_average, mapping = aes(x = reorder(Genome_Type, 
         Contig_Count_Average), Contig_Count_Average, fill = Genome_Type))  + 
         xlab("Genome") +
         ylab("Contig No.") + 
         ggtitle("Contig Count per Genome Distribution") +
         geom_bar(stat = "identity") +
         theme(text = element_text(size=20),
         axis.text.x = element_text(angle=90, hjust=1)) +
         guides(fill=guide_legend(title="Genome Type")) +
         coord_flip() +
         theme_bw() +
         scale_y_continuous(limits = c(0,2835), expand = c(0, 0)) +
         scale_x_discrete(labels = abbreviate)

p2

在此处输入图像描述

Also note that you can use geom_col instead of geom_bar(stat = "identity") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM