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
Related
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 want to change the default gray color of the bins in a histogram made with gamlss::histDist(). I've tried searching but I can't find any information on how to do it.
Here a small example from the package documentation:
require(gamlss)
data(abdom)
histDist(y,family="NO", ylim=c(0,0.005), data=abdom)
Which produces this plot:
How can I change the color of those bins?
You can not change the bin's color because there is not an available parameter for the user. You can plot by yourself the histogram and then add the density curve.
require(gamlss)
mod <- histDist(y,family="NO", data=abdom)
hist(abdom$y, freq=FALSE, col='pink')
curve(dNO(x, mu=mod$mu, sigma=mod$sigma), col=4, lwd=5, add=TRUE)
Then you can customize the plot.
You may try:
histDist(data=abdom, y,
family="NO",
main="The y and the fitted NO distribution",
xlab = "your choice",
ylab = "Density",
border.hist = "black",
ylim=c(0,0.005),
col.main = "cyan",
col.axis = "brown",
col.lab = "green",
fg.hist = "blue",
line.ty = 1,
line.col = "red",
col.hist = "pink")
getAnywhere(histDist) will show you the function and you may add some extra arguments as cex.main=cex.main, ...
Looking at this code:
pairs(Iris[1:3], main = "Anderson's Iris Data -- 3 species",
pch = c(21), cex = 2,bg = c("red","green3","blue")[unclass(iris$Species)])
is it possible to show the groups/classes Species as legend color coded?
pairs(iris[1:3], main = "Anderson's Iris Data -- 3 species",
pch = c(21), cex = 2, bg = c("red","green3","blue")[unclass(iris$Species)], oma=c(4,4,6,10))
par(xpd=TRUE)
legend(0.55, 1, as.vector(unique(iris$Species)), fill=c("red", "green3", "blue"))
From ?pairs:
Graphical parameters can be given as arguments to plot such as main. par("oma") will be set appropriately unless specified. Hence any attempts to specify par before pairs will result in override.
Additionally it is very complicated to control the legend position in pairs.
I recommend using library(GGally)
library(GGally)
ggpairs(iris, aes(color = Species), columns = 1:4)
I want to make a legend on my graph, which is generated by plot() function. The original legend() function will generate a list which has only 1 column. How can I make a legend which has 2 columns?
I could not find a way to do that within a single call to legend for standard plots.
Here's an option, drawing two separate legends: one with lines and points, one with labels. x.intersp can be used to tweak distance between labels and lines.
plot(cumsum(runif(n = 100)))
# draw legend with lines and point but without labels and box. x.intersp controls horizontal distance between lines
L = legend(x = 'bottom', legend = rep(NA,4), col=1:2, lty=c(1,1,2,2), ncol=2, bty='n', x.intersp=0.5, pch=c(1,2,1,2), inset=0.02)
# use position data of previous legend to draw legend with invisble lines and points but with labels and box. x.intersp controls distance between lines and labels
legend(x = L$rect$left, y = L$rect$top, legend = c('Group A', 'Group B'), col=rep(NA,2), lty=c(1,1), ncol=1, x.intersp = 3, bg = NA)
Check this:
library(lattice)
myPCH <- 15:17
Data <- rnorm(50)
Index <- seq(length(Data))
xyplot(Data ~ Index,
pch = myPCH, col=1:2,
key = list(space = "right", adj=1,
text = list(c("a", "b", "c"), cex=1.5),
points = list(pch = myPCH),
points = list(pch = myPCH,col=2)))
It looks like Victorp answered this in the comments of the original post. The ncol argument in the legend function works for me:
legend(locator(1), legend=c("name1","name2", "name3", "name4"), lty=2, col=c("black", "blue", "dark green", "orange"), ncol=2)
enter image description here
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)