I am plotting roughly 300 variables, R automatically sets x-axis labels and has them as numbers, I don't want this. But if I use the xaxt = 'n' feature and then 'axis', it won't work because for axis I'll need to define x labels for 300 variables to....
Is there any way to fix this?
To give a better example of my issue, my code looks something like this:
plot(data, xaxt = 'n')
x = c("Time 1", "Time 2", "Time 3", "Time 4", "Time 5", "Time 6")
axis(side = 1, at = 1:6, labels = x[1:6])
But this is not working, I think it has to do with the fact that I have 300 data points, not 6, but I just want it to equally space out these 6
at should contain 6 numbers between 1 and 300 at which you want the labels to be placed, not just 1 through 6, because this will squash them all at the beginning/left side.
dat=rnorm(300)
plot(dat, xaxt="n")
x=c("Time 1", "Time 2", "Time 3", "Time 4", "Time 5", "Time 6")
axis(1, at=floor(seq(1,300,length=6)), labels=x)
Related
I have an excel document which looks like the one below:
When I created a barplot using the base R barplot function the x-axis labels did not fit in one line since the labels were long.
example <- read_excel("C:/path/to/document/example.xlsx")
View(example)
example <- example[order(example$Quantity, decreasing = TRUE),]
P <- barplot(height=example$Quantity, names=example$`Long Name`,
col = c("red", "coral","orange", "yellow", "green", "aquamarine" , "cyan", "magenta","violetred", "pink"),
xlab = "Examples of long names",
ylab = "Quantity",
ylim = c(0,60)
)
text(P, example$Quantity+4,labels=as.character(example$Quantity)
)
I want to find a way split the labels so that it sits on different lines as I do not want to rotate the x-axis.
I tried splitting the lines by using \n and the names.arg argument:
P <- barplot(height=example$Quantity, names=example$`Long Name`,
col = c("red", "coral","orange", "yellow", "green", "aquamarine" , "cyan", "magenta","violetred", "pink"),
xlab = "Examples of long names",
ylab = "Quantity",
ylim = c(0,60),
names.arg = c("Really \n really Long \n Name 10",
"Really \n really Long \n Name 3",
"Really \n really Long \n Name 2",
"Really \n really Long \n Name 5",
"Really \n really Long \n Name 1",
"Really \n really Long \n Name 7",
"Really \n really Long \n Name 6",
"Really \n really Long \n Name 9",
"Really \n really Long \n Name 4",
"Really \n really Long \n Name 8"
))
text(P, example$Quantity+4,labels=as.character(example$Quantity)
)
But the label ends up being written over the graph.
Is there a way to either move all 3 lines down below the graph/ shift the graph up or is there a completely different way to split the lines? Ideally without having to use names.arg as it's a bit tedious to do it this way
I'm creating a figure in base R (not my preference, but that's what I've been told to do), and I'm having trouble with the spacing of the columns in the legend. When the plot is zoomed in the gap between the columns is larger than I would like, especially when there will be a lot more keys in the figure than what I've written here. Is there a parameter that controls that or a way to work around it?
plot(1, 1)
legend("bottomleft",
legend = c("Study 1: Additional Text",
"Study 2: Additional Text",
"Study 3: Additional Text",
"Study 4: Additional Text"),
pch = 16:19,
lty = 1:4,
bty = "n",
col = c("black", "red", "blue", "brown"),
ncol = 2)
I am working on creating a unified theme for a large project including many different types of plots. I have set an overall theme where the angle for axis.text.x.bottom is 0 (horizontal), however some of my x-axis labels are quite long. I would like to set the angle of axis.text.x.bottom equal to 0 for labels of length less than 3 and equal to 90 (read bottom to top) for labels of length greater than 3, without having to specify the theme for each individual plot.
*** this is a highly simplified example
library(ggplot2)
report_theme(axis.text.x.bottom = element_text(angle = 0))
testdata1 = matrix(NA, nrow = 10, ncol = 2)
testdata1[,1] = 1:10
testdata1[,2] = 1:10
testdata1 = as.data.frame(testdata1)
g1 = ggplot(testdata1, aes(x = testdata1[,1], y = testdata1[,2])) + geom_point()
g1 + report_theme
testdata2 = testdata1
testdata2[,1] = c("number 1", "number 2", "number 3", "number 4", "number 5", "number 6", "number 7", "number 8", "number 9", "number 10")
g2 = ggplot(testdata, aes(x = testdata2[,1], y = testdata2[,2])) + geom_point()
g2 + report_theme
The plot for testdata1 looks fine since the x-axis labels are numbers, however the x-axis labels for testdata2 are long strings, and hence can't easily be read horizontally because they overlap.
I know how to change the angle for each plot individually, but it requires knowing the name of the data frame being used to plot, and I would like to be able to do it within the overall theme report_theme. How can I do this when a different data frame is being used for each plot?
Im making a lot of plots in R and a part of my code looks like this:
plot(x=0,y=0, type="n", ylim=c(0,250), xlim=c(0,8), bty="n", main = "Line 20 male 3 sec rep 2",
xlab = "Concentration", ylab = "MM above buttom")
fc <- levels(dat20m2$Conc)
for(i in 1:length(fc)){
tmp <- dat20m2[dat20m2$Conc==fc[i],]
points(y=tmp$t30.sum,x=rep(i,length(tmp$t30)))
points(y=mean(tmp$t30.sum),x=i,col="red")
}
abline(lm(t30.sum~as.integer(Conc),data=dat20m2), lty=2)
My x axis is from 0:8 but what i would like it to be is A, B, C, D, E, X, Y and i can make it work with the alphabet but when i wanna jump some letters i get into troubles. Can someone help me with that?
plot(x=0,y=0, type="n", ylim=c(0,250), xlim=c(0,8), bty="n", main = "Line 20 male 3 sec rep 2",
xlab = "Concentration", ylab = "MM above buttom", xaxt="n")
## added xaxt="n"
## and the axis code below
axis(1,at=0:7,labels=c("0","A","B","C","D","E","X","Y"))
fc <- levels(dat20m2$Conc)
for(i in 1:length(fc)){
tmp <- dat20m2[dat20m2$Conc==fc[i],]
points(y=tmp$t30.sum,x=rep(i,length(tmp$t30)))
points(y=mean(tmp$t30.sum),x=i,col="red")
}
abline(lm(t30.sum~as.integer(Conc),data=dat20m2), lty=2)
I got the answer from here and I've amended your code by adding the two parts described by the double pound signs. Hope this helps.
How do I label each bar below the x-axis, for example if each bar represents a month, how do I get month one, month two, month three etc below each bar.
conditiongood <- c(50,65,60,65,59)
conditionpoor <- c(61,46,51,46,52)
condition <- rbind(conditiongood,conditionpoor)
layout(matrix(1:1,1,1))
barplot(as.matrix(condition),
col=c("darkblue","red"),
xlab="month",
ylab="subject count",
main="Monthly condition",
ylim=c(0, 140))
legend(5.25,140.1,
c("good","poor"),
fill=c("darkblue","red"),
title="condition")
You mean something like this?
Which is based on this post.
There are probably more sophisticated ways to do this.
# Barplot
bp<-barplot(as.matrix(condition),
col=c("darkblue","red"),
xlab="month",
ylab="subject count",
main="Monthly condition",
ylim=c(0, 140))
# x-axis labels
axis(1, at = bp,
labels=c("month 1", "month 2", "month 3", "month 4", "month 5"),
cex.axis=1.2)
# Add legend
legend(5.25,140.1,
c("good","poor"),
fill=c("darkblue","red"),
title="condition")
Which will give:
You probably want to do something on the position of your legend, and I don't think that specifying the xlab is necessary if you're going to label each bar individually.
I won't comment on the choice of colours :)