Labels y-axis change - r

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)

Related

Plot line on ggplot2 grouped bar chart

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))

Drawing a multiple line ggplot figure

I am working on a figure which should contain 3 different lines on the same graph. The data frame I am working on is the follow:
I would like to be able to use ind(my data point) on x axis and then draw 3 different lines using the data coming from the columns med, b and c.
I only managed to obtain draw one line.
Could you please help me? the code I am using now is
ggplot(data=f, aes(x=ind, y=med, group=1)) +
geom_line(aes())+ geom_line(colour = "darkGrey", size = 3) +
theme_bw() +
theme(plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank())
The key is to spread columns in question into a new variable. This happens in the gather() step in the below code. The rest is pretty much boiler plate ggplot2.
library(ggplot2)
library(tidyr)
xy <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10),
ind = 1:10)
# we "spread" a and b into a a new variable
xy <- gather(xy, key = myvariable, value = myvalue, a, b)
ggplot(xy, aes(x = ind, y = myvalue, color = myvariable)) +
theme_bw() +
geom_line()
With melt and ggplot:
df$ind <- 1:nrow(df)
head(df)
a b med c ind
1 -87.21893 -84.72439 -75.78069 -70.87261 1
2 -107.29747 -70.38214 -84.96422 -73.87297 2
3 -106.13149 -105.12869 -75.09039 -62.61283 3
4 -93.66255 -97.55444 -85.01982 -56.49110 4
5 -88.73919 -95.80307 -77.11830 -47.72991 5
6 -86.27068 -83.24604 -86.86626 -91.32508 6
df <- melt(df, id='ind')
ggplot(df, aes(ind, value, group=variable, col=variable)) + geom_line(lwd=2)

How to manage parameters with different length in R

I have to data sets (80211 and mine) as follows: each file has one column of data.
80211
1
2
3
4
5
mine
1
2
3
I need to read these two files and plot the cdf with ggplot2 but it says that the length of parameters are different.
The code is here.
library(ggplot2)
data1 <- read.csv('80211')
data2 <- read.csv('mine')
df <- data.frame(x = c(data1, data2), ggg=factor(rep(1:2, c(5,3))))
ggplot(df, aes(x, colour = ggg)) +
stat_ecdf()+
scale_colour_hue(name="my legend", labels=c('80211','mine'))
#Here this seems to work:
require(ggplot2)
data1 <- 1:5
data2 <- 1:3
df <- data.frame(x = c(data1, data2), ggg=factor(rep(1:2, c(5,3))))
ggplot(df, aes(x, colour = ggg)) +
stat_ecdf()+
scale_colour_hue(name="my legend", labels=c('80211','mine'))

compare different datasets with stacked bar graphs in R

I need to compare two different methods that each of them has 3 different results in one graph with using stacked bar style.
I want to draw a plot so that x axis shows the experiment and y axis shows results. and each bar fills with 3 results in stacked bar format.
experiment method resuult1 result2 result3
1 m1 1 2 3
1 m2 4 5 6
2 m1 7 8 9
2 m2 10 11 12
3 m1 13 14 15
3 m2 16 17 18
I have this code for comparing two data set how can i change it.
library(ggplot2);
pdf(file = '$filename.pdf', width=5, height=5);
data1 <- as.matrix(read.table('$INPUT_FILE1', header = T));
data1.experiment <- as.numeric(data1[,\"Experiment\"]);
data1.obs <- as.numeric(data1[,\"Result1\"]);
data1.method <- as.factor(data1[,\"Method\"]);
df <- data.frame(data1.experiment, data1.method, data1.obs);
orderlist = c("70", "100", "130", "160", "190", "260");
ggplot(df, aes(x = data1.experiment, y = data1.obs, fill = data1.method), ylim=c(60000, 2800000)) +
geom_bar(stat='identity', position='dodge')+
labs(x='$xlabel',y='$ylabel', fill='Methods') +
scale_fill_manual(values = c('red','blue'), labels = c('DTB-MAC', 'IEEE802.11P')) +
scale_x_continuous(breaks = orderlist)+
theme(legend.position = c(1, 1), legend.justification = c(1, 1), legend.background = element_rect(colour = NA, fill = 'white'));
You said that you need to compare the methods. If you represent experiment on x-axis and result on y then how will you represent method??? My way of doing it is using the facet. Here is the code for how to do it using ggplot2.
dat <- read.csv("data.csv")
library(reshape2)
library(ggplot2)
dat1 <- melt(dat,id.vars = c("experiment","method"))
p <- ggplot(dat1,aes(experiment,value,fill=variable))+geom_bar(stat="identity")+
facet_wrap(~method,nrow=1)
p
This sort of multi-dimensional chart is best explored using the ggplot2 package. I will assume here that the data you have pasted is stored in the data.frame d:
require(reshape2) ## needed to have all experiments in one variable
require(ggplot2) ## needed for the great vizualizations
d <- melt(d, id.vars=c("experiment", "method"))
ggplot(d, aes(x=factor(experiment), y=value, fill=variable)) +
geom_bar(stat="identity") +
facet_wrap(~method)
You can polish the graph further using custom labels, but that is too long to explore here. The questions with the ggplot2 tag have lots of great examples.
EDIT: Corrected to show the methods too, as already answered by #user2743244

ggplot not adding legend. What am I missing? very new to R

I'm plotting three samples with ggplot but it's not adding a legend for the samples. It's not spitting out any error message so I'm not sure where I'm going wrong. I'd really appreicate some guidance.
I've tried to declare color for each sample for the legend manually but there is still no legend on the plot.
df<-data.frame(samples$V1, samples$V2, samples$V3, samples$V4, samples$V5, samples$V6, samples$V7)
CG_methplot <- ggplot(df, aes(x=samples$V1,))+
scale_x_continuous(breaks=number_ticks(10))+
xlab("bins")+
ylab("mean CG methylation")+
geom_point(aes(y=samples$V2), size=3, colour='#009933')+
geom_point(aes(y=samples$V3), size=3, colour='#FF0000')+
geom_point(aes(y=samples$V4), size=3, colour='#0033FF')+
scale_color_manual(values=c("samples1"="009933", "sample2"="FF0000", "sample3" ="0033FF"))
CG_methplot
As requested, sample data.
head(df)
samples.V1 samples.V2 samples.V3 samples.V4 samples.V5 samples.V6 samples.V7
1 1 0.033636 0.027857 0.028830 0.029836 0.024457 0.024930
2 2 0.032094 0.029620 0.028005 0.028294 0.026220 0.024105
3 3 0.032011 0.027212 0.029728 0.028211 0.023812 0.025828
4 4 0.030857 0.029833 0.028907 0.027057 0.026433 0.025007
5 5 0.028480 0.028080 0.028553 0.024680 0.024680 0.024653
6 6 0.029445 0.027099 0.029346 0.025645 0.023699 0.025446
library(reshape2)
melted <- melt(df, id.vars = "V1")
p <- ggplot(melted, aes(x = V1, y = value, colour = variable))
p + geom_point()

Resources