I am using the following code if it helpful to plot some data using hist() in R.
hist(info$data, breaks=300, main="Some Data", xlab="data", xlim=c(0, 10000))
And this is the image I get the following image:
Is there a way to get rid of the line at y=0 that is underlining all of the bars? Or is there some better way to extend it to the y axis so it looks more like an axis rather than just some line beneath my bars?
I would use ggplot2
library(ggplot2)
ggplot(info, aes(x=data)) + geom_histogram(binwidth) +
ggtitle("Some Data") + xlab("data") + xlim(0,10000)
Related
Maybe you can help me. I need to plot a time series, let's call it, ts for that, I use the following code:
plot(ts,col="royalblue", ylab="time series plot", main = "Plot", grid.col=NA)
But I also need to plot in the same plot, a vertical line at the date 18-03-2020 (March 18, 2020). I tried with the following line of code but I don't get any vertical line:
geom_vline(xintercept = 03-18-2020, color="red", linetype="doted", size=2.5)
And also with this one:
abline(a=NULL,b=NULL,h=NULL,v="18-03-2020", col="red")
And this one:
date1 <- as.Date("2020-03-18") + 0:99
abline(v=as.Date(date1))
But with none of them I get the vertical line I need.
What am I doing wrong? could you help me?
You can do it with abline. Set v and position of x axis where you want the horizontal line. In this example v=0.4
plot(ts,col="royalblue", ylab="time series plot", main = "Plot", grid.col=NA)
abline(v=0.4, col="blue")
I try to plot a graph in R using the ggplot2 package. Let's assume I have the following data:
df1<-data.frame(xpos=c(1,2,3,4),ypos=c(0.222,0.222,0.303,0.285))
Now, I want to plot a simple line graph:
ggplot(data=df1, aes(x=xpos, y=ypos)) +
geom_line()+
geom_point()
Now, when I adjust the y-axis:
+scale_y_continuous("Y rates upd", breaks=seq(0,1,0.2),limits=c(0,1))
The lines get "broken"
That's dependent upon your graphics device that plots are sent to from R. See the same plot below when I instead export it to PDF.
I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.
How do I increase the grey plot area of a chart with one factor based axis and one numerical axis so that text labels in geom_text() plots are in view and do not extend outside the plot area?
In particular, I would like to extend the grey area to provide a margin area within the plot area that allows the text labels to appear in full.
Or is there a better way?
You can change the layout option of each ggplot using ggplot_gtable, then display all plots using grid.arrange.
library(ggplot2)
library(gridExtra)
## create a dummy ggplot
(g1 <- ggplot(mtcars, aes(wt, mpg)) +
geom_text(aes(label=rownames(mtcars)), size=6, angle=45) +
theme(plot.margin = unit(rep(1, 4), "cm")))
Obviously the text labels do not extend outside the plot area. But the following code allows just that:
gg_table <- ggplot_gtable(ggplot_build(g1))
gg_table$layout$clip[gg_table$layout$name=="panel"] <- "off"
grid.draw(gg_table)
Create a gg_table for each panel, then use grid.arrange to display all:
grid.arrange(gg_table, gg_table, gg_table, gg_table, ncol=2)
I know this is labor intensive, but you can write a function to create multiple ggplots and gg_tables to save time.
When I plot my fitted models in R, I get the output with a vertical scale bar. Can some one kindly tell me how to place a horizontal scale bar in R plots instead of vertical? Thanks in advance.
Not sure what you want to do.
Here are some possible solutions:
1) If using `library(ggplot2)
add + coord_flip()
2) If using plot
plot(x = value, y = 1:length(name),...)