ggplot not adding legend. What am I missing? very new to R - 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()

Related

ggplot Find sum of all groups and plot as line

I have a data that looks like this
Group x y
A 2 30
B 2 21
C 2 22
A 3 15
B 3 18
C 3 5
A 4 14
B 4 29
C 4 46
And create a chart with:
gg <- ggplot(mydata,
aes(x=x, y=y, fill=Group, group=Group))+
geom_line(data =mydata,
aes(x=x, y=y,colour=Group),
stat="identity",
size=1.5)
plot(gg)
I'm trying to add a fourth line that has the sum of A+B+C at every X. I've tried this but it adds 5 lines, not one with a sum. I want a line that would be y=73 when x=2, y=38 when x=3, and y=89 when x=4.
Code:
Group <- c("A", "B", "C","A", "B", "C","A", "B", "C")
x <- c(2,2,2,3,3,3,4,4,4)
y <- c(30,21,22,15,18,5,14,29,46)
mydata <- data.frame(Group,x,y)
gg <- ggplot(mydata,
aes(x=x, y=y, fill=Group, group=Group))+
geom_line(data =mydata,
aes(x=x, y=y,colour=Group),
stat="identity",
size=1.5)
plot(gg)
One way would be to generate a variable that sums all values of y by x via dplyr's group_by and mutate-functions. You can then generate your plot and add a second line geom that will show the x-specific sums.
library(tidyverse)
mydata %>%
group_by(x) %>%
mutate(sum.y = sum(y)) %>%
ggplot(aes(x=x, y=y, color=Group))+
geom_line(size=1.5) +
geom_line(aes(y = sum.y), color = "black")
Note that I changed your code by removing redundant code in the aesthetics, stat = "identity" in geom_line and all of the data = mydata specifications. These are simply not necessary.

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)

Scatter plot with ggplot

I want to do a scatter (xy) plot of variables in a melted data frame as shown below.
df
class var mean
0 x 4.25
0 y 6.25
1 x 2.00
1 y 11.00
I have tried this, but it plots 4 points. How can plot x and y?
library(ggplot2)
ggplot(df, aes(x=mean, y=mean, group=var, colour=class)) +
geom_point( size=5, shape=21, fill="white")
As Heroka pointed out, you need the data to be in a more wide type format. If the data was read in like this, you may use the following to convert it.
## you don't need this since you already have df
text = "class var mean
0 x 4.25
0 y 6.25
1 x 2.00
1 y 11.00"
df = read.delim(textConnection(text),header=TRUE,strip.white=TRUE,
stringsAsFactors = FALSE, sep = " ");df2
## use this library to switch from long-wide
library(reshape2)
df2 = dcast(df, class ~ var, value.var = "mean")
library(ggplot2)
ggplot(df2, aes(x=x, y=y, colour=class)) +
geom_point( size=5, shape=21, fill="white")

Labels y-axis change

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)

Grouped Barplot with three measures

I am trying to reproduce the following graph in R which is generated via Excel.
The CSV file has following content:
> myd
type am tae tac
1 kA 81.73212 73.07151 26.92849
2 kI 78.87324 84.50704 15.49296
3 kL 82.52184 69.91262 30.08738
4 kS 82.67147 69.31411 30.68589
5 sA 81.67176 73.31295 26.68705
6 sI 79.54135 81.83460 18.16540
7 sL 81.58485 73.66061 26.33939
8 sS 82.09616 71.61536 28.38464
The following R code creates am on the y axis, but I also want to add tae and tac.
ggplot(myd, aes(x=type, y=am)) + geom_bar(stat="identity")
Do you know how I can add this in R to have it like in the Excel diagram?
require(reshape2)
myd_molten <- melt(data = myd, id.vars = "type")
require(ggplot2)
ggplot(myd_molten, aes(x = type, y = value, fill=variable)) +
geom_bar(stat="identity", position=position_dodge())+
coord_flip()
Try
library(tidyr)
library(dplyr)
library(ggplot2)
gather(myd, Var, Val, am:tac) %>%
ggplot(., aes(x=type, y=Val))+
geom_bar(aes(fill=Var), position='dodge', stat='identity')+
coord_flip()

Resources