I have this data frame:
`Last Name` Feature Value
<chr> <chr> <dbl>
1 Name1 Resilience 1
2 Name2 Resilience 6
3 Name3 Resilience 2
4 Name1 Self-Discipline 3
5 Name2 Self-Discipline 7
6 Name3 Self-Discipline 4
7 Name1 Assertiveness 6
8 Name2 Assertiveness 7
9 Name3 Assertiveness 6
10 Name1 Activity level 4
and created a grouped barplot with the following code:
bar2 <- ggplot(team_sih_PP1, aes(x=Feature, y=Value, fill =`Last Name`)) + geom_bar(stat="identity", position="dodge") + coord_cartesian(ylim=c(1,7)) + scale_y_continuous(n.breaks = 7) +scale_fill_manual(values = c("#2a2b63", "#28d5ac", "#f2eff2")) + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
I also created a new data frame that holds the average values of the 3 Last Names in each Feature:
mean_name means
1 Action 4.000000
2 Reflection 4.000000
3 Flexibility 3.666667
4 Structure 3.666667
I want to add a line that shows the means of each Feature so that it looks something like this:
I managed to plot just the line but not in the bar chart, please help!
Assuming you have your code correct for geom_line() to add to your plot, you will not see anything plotted unless you set the group aesthetic the same across your plot (ex. aes(group=1)). This is because your x axis is made of discrete values, and ggplot does not know that they are connected with your data via a line. When you set group=1 in the aesthetic, it forces ggplot2 to recognize that the entire dataset is tied together, and then the points of your line will be connected.
I'd show using your data you shared, but it does not provide the same plots as you've shown, so here's a representative example.
x_values <- c('These', 'Values', "are", "ordered", "but", "discrete")
set.seed(8675309)
df <- data.frame(
x=rep(x_values, 2),
type=rep(c("A", "B"), each =6),
y=sample(1:10, 12, replace=TRUE)
)
df$x <- factor(df$x, levels=x_values)
d_myline <- data.frame(
x=x_values,
rando=c(1,5,6,10,4,6)
)
p <- ggplot(df, aes(x,y)) +
geom_col(aes(fill=type), position="dodge", width=0.5)
The following code will not create a line on the plot (you won't get an error either, it just won't appear):
p + geom_line(data=d_myline, aes(x=x, y=rando))
However, if you set group=1, it shows the line as expected:
p + geom_line(data=d_myline, aes(x=x, y=rando, group=1))
Related
This toy data frame represents my data.
Time Gene Value
1 0 A 1
2 1 A 2
3 2 A 3
4 0 B 1
5 1.2 B 2
6 1.7 B 2
7 2.1 B 2
8 3 B 2
Using the following code I can turn this into a line plot with two lines, one for A and one for B.
ggplot(data=Data, aes(x=Time, y=Value, group=Gene)) +
geom_line(aes(color=Gene), linetype="longdash", size=2)+
theme_classic()+
labs(title= paste("Genes over time course"),
x="Time",
y="Expression")+
theme(plot.title=element_text(size=20, face="bold",hjust = 0.5),
axis.text.x=element_text(size=10),
axis.text.y=element_text(size=10),
axis.title.x=element_text(size=15),
axis.title.y=element_text(size=15),
legend.text=element_text(size=10))
However, I would like Gene A to be represented by only dots, and Gene B to be represented by only a line. How can I accomplish this given the data?
Using data=~subset(., ...) we can control which data goes to each layer.
ggplot(Data, aes(x = Time, y = Value, color = Gene, group = Gene)) +
geom_line(data = ~ subset(., Gene != "A")) +
geom_point(data = ~ subset(., Gene == "A"))
(You can also use dplyr::select in place of subset, the results are the same.)
My data is a single column like this:
Number Assigned Row
1 1
2 1
3 2
4 1
5 2
6 3
... ...
When I plot using barplot I get what I want:
However when I use ggplot + geom_bar I get this:
This is my code for ggplot:
count <- data.frame(alldata[[xaxis]])
ggplot(data=count, aes(x="My X Axis", y="My Y Axis")) +
geom_bar(stat="identity")
versus the code I use for barplot:
counts <- table(alldata[[xaxis]])
barplot(counts,
main = xaxis,
xlab = "Percentile",
cex.names = 0.8,
col=c("darkblue","red"), beside = group != "NA")
Say this is your data:
df <- data.frame(AssRow = sample(1:3, 100, T, c(0.2, 0.5, 0.3)))
head(df)
# AssRow
#1 2
#2 1
#3 2
#4 3
#5 2
#6 2
This will get you a bar chart of the count of each Assigned Row, and colour them:
ggplot(df, aes(x=AssRow, fill=as.factor(AssRow))) +
geom_bar()
To change the labels use xlab ylab/make the background prettier:
ggplot(df, aes(x=AssRow, fill=as.factor(AssRow))) +
geom_bar() +
xlab("My X-label") +
ylab("My Y label") +
theme_bw()
Output:
I have data that resembles the following:
Ref Var Obs
A A 2
A C 6
A T 8
A G 2
C A 9
C C 1
C T 8
C G 4
T A 6
T C 1
T T 9
T G 6
G A 3
G C 1
G T 7
G G 2
And I am trying to use qplot to plot the data but I'm not sure how to display three columns of information instead of just two, and in a grouped maner. I would like to plot a bar plot with number of obs on the y axis and var on the x-axis grouped by ref. The following is the idea of what I am trying to do:
If I understood well your graphic, I suggest this:
Your data:
seq=c("A", "C", "T", "G")
df=data.frame('Ref'=rep(seq, each=4), 'Var'=rep(seq, 4), 'Obs'=rpois(16, 2))
The plot:
ggplot(data=df) + aes(x=Ref, group=Var, y=Obs) + geom_bar(stat='identity', position="dodge", fill="lightblue", color="black")
Rendering:
Or if you need to see the complete axis legends, you can use the facetting:
ggplot(data=df) + aes(x=Var, y=Obs) +
geom_bar(stat='identity', position="dodge", fill="lightblue", color="black") +
facet_grid(~Ref)
last remark: if you want to change the order of the bars, just modify the levels of the factor variables.
I struggling a lot with a graph and I dont know what is going wrong. I got the following dataframe:
And then I use the following dataframe:
df <- read.table(text ="YEAR Eucaris Niet.Eucaris
1 8 81867 0.1527756
2 9 91507 0.1533734
3 10 102755 0.1733875
4 11 116491 0.1648633
5 12 55133 0.1771800
6 13 67115 0.1449571", header =TRUE)
This works but when I expand the dataframe
r <- c(14,56849)
df <- rbind(df, r)
The graph shows 8, 10, 12 in stead of 8,9,10 etc...
Why is this happening?
Using ggplot2, by modifying scale_x_continuous:
library(ggplot2)
graph <- ggplot(df, aes(x = YEAR, y=Eucaris)) +
geom_line(linetype="dashed", size=1, colour="blue") +
geom_point(size=4, shape=22, colour="darkred", fill="pink")+
scale_x_continuous(breaks = 1:14)
I am trying to present my data using ggplot2. My dataframe is build up like this:
type count
1 exon 4
2 intron 3
3 intron 1
4 exon 10
.. ... ..
I am trying to present the data by plotting as histograms and boxplots, but I encounter some problems.
For the histograms I used the following code:
ggplot(hisdat, aes(x=count, fill=type)) +
geom_histogram(binwidth=.5, position="dodge")
and that gives me this plot:
As you can see the counts in the bottom of the plot are arranged such that 10 follows 1 and 100 follows 10. I arrange them from the first single number of the number count. How do I get it to go from 1-148?
For the boxplot I have the same trouble and on top of that my plot is not looking like a boxplot at all. Is my code wrong?
ggplot(hisdat, aes(x=type, y=count, fill=type)) + geom_boxplot()
It gives me this result:
since the other part of your question has already been answered in the comments here is the answer to this part:
How do I get it to go from 1-148?
df <- read.table(header = TRUE, text=
" type count
1 exon 4
2 intron 3
3 intron 1
4 exon 10")
library(ggplot2)
library(ggplot2)
ggplot(df, aes(x = reorder(type, count), y = count, fill = type)) + geom_bar(stat = "identity", position = "dodge")