I want to plot 3 barplots together in one graph based on values from different columns of a data frame.
It should look something like this.
The y-values of plot 1 are the sum of the y-values of plot 2 and 3. The color of plot 1 and 2 can be fully filled (e.g. blue & red), but the color of plot 3 has to be translucent.
I was able to make a plot for each column separately using the barplot() function, but I was not able to combine them in one graph.
barplot(covpatient[[1]]$cov, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "blue", col = "blue")
barplot(covpatient[[1]]$plus, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "red", col = "red")
barplot(covpatient[[1]]$min, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = "gray", col = "gray")
Could someone give me a hand?
I'm not exactly sure if this is what you want... but based on the graphic that you sent I think this will help:
require(ggplot2)
require(reshape2)
covpatient <-list()
covpatient$cov <-rnorm(100,2)
covpatient$plus <-rnorm(100,4)
covpatient$min <-rnorm(100,1)
plot_covpatient <- do.call(rbind,covpatient)
melted_plot_covpatient<-melt(plot_covpatient,value.name = 'Value')
ggplot(melted_plot_covpatient,aes(group=Var1))+
geom_density(aes(Value,colour=Var1,fill=Var1),alpha=.5)
Related
I have created a pie chart and I realize that the title of the pie chart is further away from the pie chart. I want to ask how I can shorten the distance between the title and the chart, using base R.
pie(group3$count, labels = a, col = c("orange", "pink"), main= "The percentage of Fuel type, among Low Emission cluster", cex.main=1 )
legend("bottomright", legend = c("DIESEL/ELECTRIC", "PETROL/ELECTRIC"), fill=c("pink","orange"))
Output:
Many thanks.
You could build the plot separately using legend() and title() and in title call line = 0.6 or any value lower than 1 to move the title closer.
Also I think you are mixing up the colours for either the fill or the legend. one has pink before orange, and the other orange before pink.
automatic <- sum(mtcars$am) / nrow(mtcars)
manual <- 1 - automatic
df <- data.frame(count = c(automatic, manual))
a <- c("Automatic", "Manual")
pie(df$count, col = c("pink", "orange"))
legend(x = "bottomright",
legend = a,
fill = c("pink", "orange"))
title(main = "TITLE", cex.main = 1)
title(main = "TITLE, BUT CLOSER", cex.main = 1, line = 0.6)
Created on 2023-01-09 with reprex v2.0.2
i have the following plot:
p<- plot(c(0,1),c(0,1), type= "n", xlab = "", ylab = "", axes = FALSE)+
rect(0,0,.5,.5, col = "green")+
rect(.5,0,1,.5, col = "orange")+
rect(0,.5,.5,1, col = "yellow")+
rect(.5,.5,1,1, col = "red")
however instead of manually imputing the colors to the rectangles i would like to have a function [0,1] -> colors, where i have a value between 0 and 1 and it colors the boxes depending on the value. e.g:
(...)
rect(0,0,.5,.5, col = function(0.3))+
(...)
Heres one way to create that function
PercentageColour <- function(x){colorRampPalette(c('green','orange','yellow','red'))(101)[round(x*100)+1]}
p<- plot(c(0,1),c(0,1), type= "n", xlab = "", ylab = "", axes = FALSE)+
rect(0,0,.5,.5, col = PercentageColour(0.01))+
rect(.5,0,1,.5, col = PercentageColour(0.25))+
rect(0,.5,.5,1, col = PercentageColour(0.356))+
rect(.5,.5,1,1, col = PercentageColour(0.95))
Where colorRampPalette(colours) creates the ramp, (100) specifies how many steps you want in the ramp, and [round(x*100)] is rounding your percentage input and converting it to the indexed values for reference.
You can also use some of the default colour ramps if you don't want to make your own. Try rainbow or heat.colors for example
Just a minor question. I am trying to make a legend for the following plot.
# fitting the linear model
iris_lm = lm(Petal.Length ~ Sepal.Length, data = iris)
summary(iris_lm)
# calculating the confidence interval for the fitted line
preds = predict(iris_lm, newdata = data.frame(Sepal.Length = seq(4,8,0.1)),
interval = "confidence")
# making the initial plot
par(family = "serif")
plot(Petal.Length ~ Sepal.Length, data = iris, col = "darkgrey",
family = "serif", las = 1, xlab = "Sepal Length", ylab = "Pedal Length")
# shading in the confidence interval
polygon(
c(seq(8,4,-0.1), seq(4,8,0.1)), # all of the necessary x values
c(rev(preds[,3]), preds[,2]), # all of the necessary y values
col = rgb(0.2745098, 0.5098039, 0.7058824, 0.4), # the color of the interval
border = NA # turning off the border
)
# adding the regression line
abline(iris_lm, col = "SteelBlue")
# adding a legend
legend("bottomright", legend = c("Fitted Values", "Confidence Interval"),
lty = c(1,0))
Here's the output so far:
My goal is to put a box in the legend next to the "Confidence Interval" tab, and color it in the same shade that it is in the picture. Naturally, I thought to use the pch parameter. However, when I re-run my code with the additional legend option pch = c(NA, 25), I get the following:
It is not super noticeable, but if you look closely at the padding on the left margin of the legend, it actually has decreased, and the edge of the border is now closer to the line than I would like. Is there any way to work around this?
That's a curious behavior in legend(). I'm sure someone will suggest a ggplot2 alternative. However, legend() does offer a solution. This solution calls the function without plotting anything to capture the dimensions of the desired rectangle. The legend is then plotted with the elements you really want but no enclosing box (bty = "n"). The desired rectangle is added explicitly. I assume you mean pch = 22 to get the filled box symbol. I added pt.cex = 2 to make it a bit larger.
# Capture the confidence interval color, reusable variables
myCol <- rgb(0.2745098, 0.5098039, 0.7058824, 0.4)
legText <- c("Fitted Values", "Confidence Interval")
# Picking it up from 'adding a legend'
ans <- legend("bottomright", lty = c(1,0), legend = legText, plot = F)
r <- ans$rect
legend("bottomright", lty = c(1,0), legend = legText, pch = c(NA,22),
pt.bg = myCol, col = c(1, 0), pt.cex = 2, bty = "n")
# Draw the desired box
rect(r$left, r$top - r$h, r$left + r$w, r$top)
By the way, I don't think this will work without further tweaking if you place the legend on the left side.
I can create a dendrogram using
x<-1:100
dim(x)<-c(10,10)
set.seed(1)
groups<-c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
x.clust<-as.dendrogram(hclust(dist(x)))
x.clust.dend <- x.clust
labels_colors(x.clust.dend) <- groups
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = groups, edgePar = "col") # add the colors.
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = 3, edgePar = "lwd") # make the lines thick
plot(x.clust.dend)
However I want to delete the scale of height information in the left as shown in Figure below.
My guess is that it should be extremely trivial but I am not able to find a way to do this. One solution which I don't want is using the ggplot2 as below:
ggplot(as.ggdend(dend2))
This is because I will loose some of the formatting like color_bars()
The graphical parameter 'axes = FALSE" can be used to remove the distance measure for the plot.dendogram command:
plot(x.clust.dend, axes=F)
This will produce the following dendogram without distance axis:
You can just set yaxt = "n"
plot(x.clust.dend, yaxt = "n")
You can add another axis with
axis(side = 2, labels = FALSE)
I made 13 parallel coordinate plots lines, where each plot has x lines, each of 5 points. There are three things that I would like to change:
I would like to remove very long vertical x-axis ticks that protrude below out of the graph
I would like to change the x-axis labels of each plot to be "N", "1", "2", "3", "4"
I would like the y-axis to be labelled for each plot. It currently is not. The maximum y-value for each plot is max(input). So, I like four y-axis labels: max(input), 3/4 max(input), 1/2 max(input), and 1/4 max(input) (all to the nearest integer to keep it neat).
I would like a main title over all the graphs (I'll just call it "Main Title" for now)
Here is my code currently:
par(mfrow = c(3,5))
par(mar=c(0.1,0.1,0.1,0.1))
# For each color (cluster) in the random network
for (i in 1:max(net$colors)){
color = mergedColors[which(net$colors == i)[1]]
input = countTable[which(net$colors==i),]
parcoord(input, lty = 1, var.label = FALSE, col = color)
}
where the str(input) is a data.frame of x observations of 5 variables.
I tried to add things like x.label = c("N","1","2","3","4"), but that did not work.
Edit:
Here is some sample data, as per suggestions. Please let me know if I should include anything else:
net <- data.frame(colors=as.numeric(sample(1:15, 100, replace = T)))
mycols <- c("brown", "blue", "turquoise", "greenyellow", "red",
"pink", "green", "yellow", "magenta", "black","purple",
"tomato1","peachpuff","orchid","slategrey")
mergedColors = mycols[net$colors]
countTable <- data.frame(matrix(sample(1:100,100*5, replace=T),
ncol=5, dimnames=list(NULL, c("Norm","One","Two","Three","Four"))))
OK. I'm not sure I understand request 1, but here's what I came up with so far
library(MASS)
opar<-par(no.readonly=T)
par(mfrow = c(3,5))
par(oma=c(1.2,2,2,0))
par(mar=c(2,2,0.1,0.1))
# For each color (cluster) in the random network
for (i in 1:max(net$colors)){
color = mergedColors[which(net$colors == i)]
input = countTable[which(net$colors==i),]
colnames(input)<-c("N",1:4)
parcoord(input, lty = 1, var.label = FALSE, col = color)
axis(2,at=seq(0,1,length.out=5),labels=seq(min(input),max(input), length.out=5))
}
mtext("Main Title",3, outer=T)
par(opar)