-2

I am completely new to programming and R, so be gentle on me ;-) - after extensive research on my problem, I still cannot figure out how to solve it...

I want to display the domains of a protein using ggplot2. I thought the best way is to use phyloseq::plot_bar() but somehow I cannot manage to do so.

I tried to compute a stacked bar plot to visualize my protein but ended up with domains in a non-specific order.

My data table looks as follows:

Data table used for analysis

When I use ggplot to compute the bar plot, this is shown:

ggplot(my_data, aes(x="", y=length, fill=description)) + 
  geom_bar(stat="identity")

Bar Plot of Domain sequence

However, I need a linear visualization, that displays the length of each domain (description) and keeps the order according to their individual starting point set in "start".

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Heiko
  • 65
  • 4
  • 1
    Welcome to the site, please improve your post as stated [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Jun 06 '19 at 15:12

1 Answers1

0

If I understand correctly, the following is more or less what you want.

library(ggplot2)
library(dplyr)

my_data %>%
  mutate(desc = sprintf('%03d%s', start, description)) %>%
  ggplot(aes(x = "", y = length, fill = desc)) +
  geom_bar(stat="identity") +
  scale_fill_manual(name = 'Description',
                    values = my_data$description,
                    labels = my_data$description) +
  xlab(NULL)

enter image description here

Data.

my_data <-
structure(list(start = c(1, 48, 175, 196, 308, 510, 526), 
length = c(47, 127, 21, 112, 202, 16, 10), description = 
structure(c(1L, 2L, 3L, 2L, 4L, 2L, 3L), .Label = c("A", 
"B", "C", "D"), class = "factor")), row.names = c(NA, -7L),
class = "data.frame")
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66