Changing y label plyr R studio - r

pretty new to R studio and R in general, I have a 3d scattered plot using the library scatterplot3d
The snip of code I have as follows:
s3d <- scatterplot3d(averageTime$Score, averageTime$Status, averageTime$Time, pch=16, highlight.3d=TRUE, type="h", main="3D Scatterplot", ylab="status", xlab= "Score", zlab="Time")
Right now the y axis comes out as 0-5 but I want to fill in custom text to replace it so as opposed to 0 I want the text "NA", 1 I would want "Covered"ect
Ive already tried applying the previous question seen here but to no avail: R: Replace X-axis with own values
please let me know if there is an easier application. Thank you

the answer is:
scatterplot3d(1:10, 1:10, 1:10, y.ticklabs=c("NA", "Covered", "Blah", "blub", "hmpf", "etc."))

Related

Issues with axis labeling on boxplots in R

Hopefully this is a quick fix. I am trying to make boxplots of nutrient river concentrations using R code written by the person previously in my position (and I am not so experienced with R, but we use it only for this). The issue is that the output boxplot axis have multiple overlapping text, some of which seems to come from another part of the code which I thought did not dictate axis labels. The original code is shown below (the working directory is already set and csv files imported and I know that works), and the resulting boxplot is in 1.
Edit: Code below
png(filename="./TP & TN Plotting/Plots/TN Concentration/Historical TN Concentrations Zoomed to Medians.png",
width=10,
height=4,
unit="in",
res=600)
par(mar=c(5,5,3,1),
cex=.75)
tnhistconc<-(boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist))
boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist,
ylim=c(0,3000),
xaxt="n"),
at=c(1:3,5:8,10:(length(tnhistconc$n)+2)))
axis.break(axis=1,breakpos=c(4),style="slash")
axis.break(axis=1,breakpos=c(9),style="slash")
text(c(1:3,5:8,10:(length(tnhistconc$n)+2)),
-50,
paste("n=",
tnhistconc$n),
cex=0.8)
title(ylab="TN Concentration (ppb)",
xlab="Year")
title(main=paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(TNhist$Year),") TN Concentration"))
dev.off()
I made the edit of adding ,xlab="",ylab="" after xlab="Year" towards the bottom, since this fixed this issue in other similar sections of boxplot code (except it seems I needed to add it to a different part of those sections, see 2 - also tried it after xaxt ="n" as in 2 and got the same result). It fixes the overlapping text issue, but the axis labels are still not what I want them to be ("Year", and "TN Concentration (ppb)), and this is shown in 3.
So, does anyone potentially know of a simple fix that might get rid of these unwanted labels and replace them with the correct ones? Am I missing something basic? The same original code seemed to work fine in the past before I was doing this (for 2018 data), and the spreadsheets the data is being imported from are the same, same setup and everything. Many thanks in advance!
Edit: I have a sample dataset which is just the last 2 years of data. See here: https://docs.google.com/spreadsheets/d/10oo9w-IzXkLWdY10A9gHYhDH67MeSibBpc2q67L6o88/edit?usp=sharing
Original code result
How this code fixed other similar issues
Partially fixed result based on edit
I don't know if it is a typo but you have an extra parenthesis after xatx = "n".
Maybe you can try something like that:
png(filename="./TP & TN Plotting/Plots/TN Concentration/Historical TN Concentrations Zoomed to Medians.png",
width=10, height=4, unit="in", res=600)
par(mar=c(5,5,3,1), cex=.75)
tnhistconc<-(boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010], data=TNhist))
boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist,
ylim=c(0,3000),
xaxt="n", ylab = "", xlab = "",
at=c(1:3,5:8,10:(length(tnhistconc$n)+2)))
axis.break(axis=1,breakpos=c(4),style="slash")
axis.break(axis=1,breakpos=c(9),style="slash")
text(c(1:3,5:8,10:(length(tnhistconc$n)+2)),
-50,
paste("n=",
tnhistconc$n),
cex=0.8)
title(ylab="TN Concentration (ppb)",
xlab="Year",
main=paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(TNhist$Year),") TN Concentration"))
dev.off()
xatx will remove the x axis (that will control by axis.break. xlab and ylab will remove x and y axis title and they will be set later by title.
Hopefully, it will works
EDIT: Using ggplot2
Your dataframe is actually in a longer format making it easily ready to be plot using ggplot2 in few lines. Here your dataset is named df:
library(ggplot2)
ggplot(df, aes(x = as.factor(Year), y = Conc_ppb))+
geom_boxplot()+
labs(x = "Year", y = "TN Concentration (ppb)",
title = paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(df$Year),") TN Concentration"))

R Plot Adds Extra Unwanted Line

I'm generating a simple line plot in R, however it adds another unwanted straight horizontal line to my plot that I don't want. And it happens in all of my line plots. I have tried google, however it only gives me instructions on how to add an extra line and not why this is happening. I am using RStudio 0.98.1028 on Mac OS X Yosemite.
plot(data2$interval,data2$steps,main="Plot of Average Activity",
xlab = "Interval", type="l", ylab="Average steps taken")
I guess the problem is with your data. You might have rows at the end of the data frame that "return" to the origin. Here you have a reproducible example:
data2 <- data.frame(interval = 1:200, steps = rnorm(200, 50, 20))
data2[1,2] <- 0
data2[200,2] <- 0
data2[201, ] <- c(0, 0)
plot(data2$interval,data2$steps,main="Plot of Average Activity",
xlab = "Interval", type="l", ylab="Average steps taken")
please vote if the answer is fine with you :)

plot several linegraphs in one image using R

I am an absolute beginner in R. so this is probably a stupid question.
I have a table like this (csv format):
,1A+,2A+,3A-,3A+,5A-,5A+,6A-,6A+,7A-,7A+
6,4.530309305,5.520356001,3.437626731,5.146758132,,4.355022819,,4.191337618,,4.076583859
10,8.697814022,9.765817956,,9.636004092,3.725756716,8.600484774,3.457423715,8.358842335,2.246622784,7.244668991
12,,,8.176341701,,,,,,,
17,,,,,6.24785396,,5.077069513,,3.137524578
I want to create a line graph in R plotting all the different Y values (1A+, 2A+, etc) vs the Y values (6,10,12,17).
I am doing:
new_curves <- read.csv("new_curves_R.csv", as.is = TRUE)
g_range <- range(0,new_curves$X)
axis(2, las=1, at=4*0:g_range[2])
plot(new_curves$X1A.,new_curves$X,type="o", col="blue")
legend(1, g_range[2], c("new_curves$X1A."), cex=0.8, col=c("blue"));
title(xlab="Days", col.lab=rgb(0,0.5,0))
title(ylab="Total", col.lab=rgb(0,0.5,0))
However, this (obviously) only plots the first datapoint. (the legend is not working for some reason either). I am guessing I need some sort of for loop to add each Y value to the graph recursively. Likewise, a loop would be needed to make the legend.
thanks
dat <- read.table(text=", 1A+,2A+,3A-,3A+,5A-,5A+,6A-,6A+,7A-,7A+
6,4.530309305,5.520356001,3.437626731,5.146758132,,4.355022819,,4.191337618,,4.076583859
10,8.697814022,9.765817956,,9.636004092,3.725756716,8.600484774,3.457423715,8.358842335,2.246622784,7.244668991
12,,,8.176341701,,,,,,,
17,,,,,6.24785396,,5.077069513,,3.137524578", header=TRUE, sep=",", fill=TRUE)
matplot(dat[1], dat[-1])

How to read russian text on adobe windows (R plot)?

I m working on R plot with Russian label but on windows (adobe) russian text are not visible. Do i need to install some package to view it.
pdf("sample.pdf",width = 6.6 ,height = 4.2,family= "URWHelvetica", encoding="KOI8-R")
x<-c(1,2,3,4,5)
y<-c(2,3,4,5,6)
xlable<-c("ручка","книга","часы","ложка","смотреть")
plot(x,y,xlab=xlable)
dev.off()
Regards
It looks like you're trying to label the points. But you're telling R to write what you want as point labels as an x-axis label. xlab is the label for the x-axis.
I think you're really just looking for text()
Try this instead, for example:
x <- c(1,2,3,4,5)
y <- c(2,3,4,5,6)
xlable<-c("ручка","книга","часы","ложка","смотреть")
plot(x,y,xlab=xlable)
text(x,y,xlable,pos=c(4,1,1,1,2))
If you need labels under the x axis, then you need to use axis() function. This should work
pdf("sample.pdf",width = 6.6 ,height = 4.2,family= "URWHelvetica", encoding="KOI8-R")
x<-c(1,2,3,4,5)
y<-c(2,3,4,5,6)
xlable<-c("ручка","книга","часы","ложка","смотреть")
plot(x,y,xaxt="n")
axis(1,at=1:5,labels=xlable)
dev.off()

label of log y-axis: 1000 instead of 1e+03?

I've a problem concerning construction of log y-axis in a graphic.
How can I manage that the units/numbers of my log y-axis aren't shown in
1e+03, 1e+04, 1e+05 etc....
But only in regular Arabic numbers (1000, 10000, 100000)?
You need to remove the axis (by setting yaxt = "n") and then re-format it properly:
plot((1:100)^3, log = "y", yaxt = "n")
axis(2, format(c(1,10,100)^3, scientific=FALSE))
This was asked before on R-help.
Additionally, if you just don't like the look of 1e+03 scientific notation, the sfsmisc package has the axTexpr() function to format axis labels in a * 10^k notation.
library(sfsmisc)
example(axTexpr)
As I understand the question, the original poster wanted to get rid of the scientific notation of the labels. I had the same problem and found out this one works for that purpose (without using package sfsmisc from Kevin's answer, which I did not try):
plot((1:100)^3, log = "y", yaxt = "n")
axis(2, at=axTicks(2,log=TRUE), labels=format(axTicks(2, log=TRUE), scientific=FALSE))
This is rather late, but I was searching for the same solution. What I did (by searching, trial and error) is:
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
majorFormatter = FormatStrFormatter('%d') # shows 1 instead of 10^0 etc
and later, in the plot creating process:
ax = subplot(111) # ie one plot, but need to refer to it as 'ax'
semilogy(x,y)
and just before show(),
ax.yaxis.set_major_formatter(majorFormatter)
There may be unnecessary bits here, as I'm a rank Python newbie.

Resources