I am trying to do a scatter plot of 2 time series data - the data is stored in a data frame. The background of the image is quite grainy and axis labels are not visible when I do:
ggplot(data=dat,aes(x,y))+geom_point()
With below, I get only dark vertical lines:
plot(dat$x,dat$y)
plot() and ggplot() did work after applying as.numeric() to the data(as below) but the axis labels are indices[1,2,...] and not the range of actual values.
plot(as.numeric(dat$x),as.numeric(dat$y))
ggplot(data=dat,aes(as.numeric(x),as.numeric(y)))+geom_point()
I cannot post the images here as I am new to this forum.
By default, the data was getting converted into factor while converting from matrix to data.frame. Below code fixed it.
data.frame(mydata,stringsAsFactors = FALSE)
Related
I'm trying to put a legend on a line graph using legend(x,y, legend=c("","").... etc. I've changed the date to numeric data and used that for x and it plots, so I know the rest of its right. but when x is a date I'm not sure what to use for x to get the legend to show on the graph.
thanks
I am trying to put labels beside some points which are very close to each other on geographic coordinate. Of course, the problem is overlapping labels. I have used the following posts for reference:
geom_text() with overlapping labels
avoid overlapping labels in ggplot2 charts
Relative positioning of geom_text in ggplot2?
The problem is that I do not want to relocate labels but increase the interval of labeling (for example every other 10 points).
I tried to make column as alpha in my dataframe to make unwanted points transparent
[![combined_df_c$alpha=rep(c(1,rep(0,times=11)),
times=length(combined_df_c$time)/
length(rep(c(1,rep(0,times=11)))))][1]][1]
I do not know why it does not affect the plot and all labels are plotted again.
The expected output is fewer labels on my plot.
You can do this by sequencing your dataframe for the labs of geom_text.
I used the build-in dataset mtcars for this, since you did not provide any data. With df[seq(1,nrow(df),6),] i slice the data with 6-steps. This are the labels which get shown in your graph afterwards. You could use this with any steps you want. The sliced dataframe is given to geom_text, so it does not use the original dataset anymore, just the sliced one. This way the amount of points for the labels and the amount of labels are equal.
df <- mtcars
labdf<- df[seq(1,nrow(df),6),]
ggplot()+
geom_point(data=df, aes(x=drat, y=seq(1:length(drat))))+
geom_text(data=labdf,
aes(x=drat, y=seq(1:length(drat))), label=labdf$drat)
The output is as expected: from 32 rows, just 6 get labeled.
You can easily adjust the code for your case.
also: you can put the aes in ggplot() which may be more useful if you use more then just gemo_point. I made it like this, so i can clarify: there is a different dataset used on geom_text()
I am trying to plot data with a categorical x-axis variable and a continuous y-axis variable. The current plot is shown below:
A am trying to alter the height of the y-axis to make the plot taller and thinner. I know you can do this kind of thing with a continuous variable vs. continuous variable scatterplot using coord_fixed(), e.g.:
However, this works on the numeric ratios of the x and y data, which doesn't apply if one variable is categorical. Trying coord_fixed on my data with any input seems to just scale the plot exactly to the plot area:
Trying to adjust the canvas at the ggsave phase just adds white space around the plot, rather than changing the plot shape itself, which isn't what I'm looking for either:
ggsave(filename = paste(imgSaveDir,'RT_data_summ.png',sep=""),width=7,height=10)
Any help is appreciated!
I'm trying to make a plot in R. My x-axis is a week number converted to factor and my y-axis is an amount.
When I run plot() instead of dots I get horizontal lines.
Why does this happen?
Here is a sample dataset:
df <- data.frame(fin_week=as.factor(seq(1,20, by =1)), amount=(rnorm(20)^2)*100)
plot(df)
Looking at the documentation, it's because the first column is a factor. When R tries to find the right plot() to run, it looks into plot.dataframe, where it plots on the type of 1st column i.e a factor. Hence it plots using plot.factor(), which gives a line by default, which is used for box plots.
try using plot.default(df) to plot and you should get it the scatter plot
I want to add multiple vertical lines to a time series plot.
Normally you would specify abline(v=x-intercept) but my x-axis is in the form 01/08/2000 – 31/07/2001. How would I adapt the abline code to add vertical lines for example 13/10/2000 and 30/05/2001?
Firstly, I have tried the following zoo plot but the x-axis is not giving me the preferred date format;
dat<-read.table("Aug2000-July2001.txt", sep="\t", header=T)
z <- with(dat, zoo(cbind(NEE,SWC)))
options("na.actions"=na.omit)
plot.zoo(z, ylab=c("NEE(umolm-2s-1)", "SWC(%)"),
col=c("black","blue"), xlab=c("Date"), lwd=2)
Secondly, I have also tried e.g.
abline(v=as.Date("13/10/2000"))
but it is not working. How do I add multiple vertical lines and how do I add my date format on the x-axis on a zoo plot?
I have a table with some of the columns as follows: