This question already has an answer here:
Reduce size of legend area in barplot
(1 answer)
Closed 4 years ago.
I was trying to create a chart using below code in R:
myplot<-function(data,rows,Colm){
Data<-data[rows,Colm,drop=F]
matplot(t(Data), type = "b", pch=15:18, col = c(1:4, 6))
legend("bottomleft",inset = 0.01, legend = Players[rows],col = c(1:4, 6),
pch=15:18, horiz = F)
}
myplot(Games)
and got the below plot:enter image description here
Can anyone help me how to reduce the legend size?
The answer can be found here: Reduce size of legend area in barplot
The key is the cex argument in the legend function call.
The default is the equivalent of 1. Larger than the default size is greater than 1. From your question, you probably want to put the value as .5 and experiment from there until it is the size you want.
Related
This question already has an answer here:
Show element values in barplot
(1 answer)
Closed 4 years ago.
I have a barplot with grouped bars. Is it possible to include a label for each bar ? Example of plot without bar labels:
test <- structure(c(0.431031856834624, 0.54498742364355, 0.495317895592119,0.341002949852507, 0.40229990800368, 0.328769657724329,0.258600583090379,0.343181818181818, 0.260619469026549), .Dim = c(3L, 3L), .Dimnames = list(
c("2015", "2016", "2017"), c("a", "b", "c")))
barplot(test,ylim=c(0,1),beside=T)
p <- barplot(test, ylim=c(0, 1), beside=T)
text(p, test + .05*sign(test), labels=format(round(test, digits=2), nsmall=2))
The last line adds the labeling over the bar plots.
p takes the return values of the barplot() which are the x-axis bar positions.
In this example this is of the format 3x3 matrix.
text() needs then p for his x= argument. And for his y= argument it needs a slightly offsetted value than its bar plot heights (test). sign() determines the direction (above or below, +1 or -1) of the bar and .05 I determined empirically by trying, it is dependent on your values of the table.
So, x= and y= are the x and y coordinates for the labeling.
And finally, labels= determines which text should be printed.
The combination of format() and round() gives you full control over how many digits you want to display and that the display is absolutely regular in turns of number of digits displayed, which is not, if you use only round().
With xpd=T you could determine, whether labeling is allowed to go outside of region or not.
cex= could determine the fontsize of the label,
col= the colouring and font= the font.
alternatively, you can give just test for y= and determine via pos=3 that it should be above and offset=1 how many characterwidths the offset of the text shoul be.
p <- barplot(test, ylim=c(0, 1), beside=T)
text(x=p, y=test, pos=3, offset=1, labels=format(round(test, digits=2), nsmall=2))
You can find plenty of more instructions by looking into the documentation by
?text
# and
?barplot
in the R console
You can add a label using text function by extending your barplot. You can play with the parameters as you wish. Here is the sample code and its output.
x= barplot(test,ylim=c(0,1),beside=T)
text(x, test, labels=test, pos=1, offset=.5, col="red", srt = 90) #srt is used for vertical labels
If you really want to make a better plot, I would recommend ggplot as it has several other features like adding a theme to your plots and it is more easy for customizations.
If you're looking to label ever bar's category not it's value you can do something like this
allPermutations <- unlist(lapply(colnames(test), function(x) paste(x, rownames(test)) ))
barplot(test,ylim=c(0,1),beside=T, names.arg = allPermutations, las=2)
the file line gets all the combinations of categories. The plot call allows you to specify individual values with "names.arg" while las=2 rotates the names so it shows a bit nicer
This question already has answers here:
Shading a kernel density plot between two points.
(5 answers)
Closed 5 years ago.
I've used the following to plot a Weibull distribution W(2,3):
Weibull=function(x){
(2*x/9)*exp(-(x^2)/9)
}
xx=seq(from=0,to=10,length.out=1000)
xx1=Weibull(xx)
plot(xx,xx1,type="l")
polygon(xx, xx1, col = "red", border = NA)
PDF plot
I want to shade the region between (4,10) only. How would I go about doing this?
xx2=seq(from=4,to=10,length.out=1000)
yy2=c(Weibull(xx2),0)
xx2=c(xx2,4)
plot(xx,xx1,type="l")
polygon(xx2, yy2, col = "red", border = NA)
This question already has an answer here:
Manually defining the colours of a wireframe
(1 answer)
Closed 4 years ago.
Is there a way to change the drape color scale in lattice: wireplot? I have a series of plots and would like to adjust the colors so they each show the same color scale. I am able to change the z axis scale, but that only makes it so the surface itself moves up and down, not the color scale. Is there a keyword I am missing when looking it up?
wireframe(InitFe2solid.ug.gdw~Depth*Distance,
allplatesdata[allplatesdata$UseMonth=="August", ],
drape = TRUE,
colorkey = TRUE,
zlab = list("Sulfate uM", rot = 90),
zlim = range(seq(0.0, 4)))
Adding the line fixes it
at=do.breaks(c(0,14),100)
This question already has an answer here:
change plotted segment ends from round to flat
(1 answer)
Closed 5 years ago.
I made a histogram plot in R using plot(type = 'h').
plot(type='h', y =runif(1:10), x = 1:10,lwd = 20,lty=7)
I want the vertical bars to be thick, so I increase lwd.
However, the larger "bars" (really, they're lines) have rounded edges:
Is there a way to make these "bars" (i.e., lines) have square/straight edges?
I've gone through the plot documentation numerous times and couldn't find a solution. No SE or Google Searches turned up anything either...
Use lend = 1.
plot(type='h', y =runif(1:10), x = 1:10,lwd = 20,l ty = 7, lend = 1)
And output
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Is there any method to keep the curves inside of x and y axis without changing the original data?
With most plotting methods, your plot will stay "inside the lines" unless you set par(xpd = TRUE) (or NA). So, you probably set that option (or passed it to your line plotting command. You can revert to the default by running the line par(xpd = FALSE).
From ?par:
xpd
A logical value or NA. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region. See also clip.
par(xpd = NA)
plot(1, type = 'n')
abline(h = 1)
p <- par('usr')
do.call('clip', as.list(p))
abline(h = 1.1)
## or equivalently
clip(p[1], p[2], p[3], p[4])
abline(h = .9)
## xpd is still NA
par('xpd')
# [1] NA