Horizontal scale bar in R plot - r

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),...)

Related

R ggplot reverse axes and control direction of bars

I am trying to mimic two types of plots (two images provided) where the negative values are to the right and positive to the left. I have sample-code below:
tdf<-data.frame(prcnt=c(-50,25,-80,5,10,-40),nm=c('AB','BC','CD','DE','EF','FG'),catg=c(rep('catA',2),rep('catB',2),rep('catC',2)))
ggplot(tdf,aes(nm,prcnt,fill=catg))+geom_col()+scale_y_continuous(limits=c(-100,100))+coord_flip()+scale_y_reverse()
How can I also have it mean-centered (at 0) like this? Thanks.
You could get the top plot with something like
ggplot(tdf,aes(ymin=as.numeric(nm)-.45,ymax=as.numeric(nm)+.45,
xmin=100, xmax=prcnt,fill=catg))+
geom_rect() +
scale_y_continuous(breaks=as.numeric(tdf$nm),
labels=levels(tdf$nm))+
scale_x_reverse(limits=c(100, -100))
and you can get the bottom plot with
ggplot(tdf,aes(nm,prcnt,fill=catg))+
geom_col()+
scale_y_reverse(limits=c(100,-100))+
coord_flip()

Flip ggplot axis so that it makes more sense

I have the below plot
However the axis on the x axis is wrong, the 1 should be on the other side, however the actual plot itself should not move...
I used the following line;
ggroc(list(ROC_base = roc_base, ROC_optimised = roc_optimised))
I had a similar problem with the base R plot previously here. Using legacy.axes = TRUE solved the problem but I cannot find a solution in ggplot
from the ggroc documentation (https://www.rdocumentation.org/packages/pROC/versions/1.11.0/topics/ggroc.roc) it looks like the solution should be the same as your solution with base R. Just add legacy.axes = TRUE
I believe you want to add scale_x_reverse() to your ggplot. This will flip the x-axis of the plot so that 1 is to the right and 0 is to the left. I haven't used ggroc before, but the normal way to add layers and options like this to a ggplot is with a +:
ggroc(list(ROC_base = roc_base, ROC_optimised = roc_optimised)) +
scale_x_reverse()

R plot and barplot how to fix ylim not alike?

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 to plot matrix with background color varying according to entry?

I wanted to ask for any general idea about plotting this kind of plot in R which can compare for example the overlaps of different methods listed on the horizontal and vertical side of the plot? Any sample code or something
Many thanks
A ggplot2-example:
# data generation
df <- matrix(runif(25), nrow = 5)
# bring data to long format
require(reshape2)
dfm <- melt(df)
# plot
require(ggplot2)
ggplot(dfm, aes(x = Var1, y = Var2)) +
geom_tile(aes(fill = value)) +
geom_text(aes(label = round(value, 2)))
The corrplot package and corrplot function in that package will create plots similar to what you show above, that may do what you want or give you a starting point.
If you want more control then you could plot the colors using the image function, then use the text function to add the numbers. You can either create the margins large enough to place the text in the margins, see the axis function for the common way to add text labels in the margin. Or you could leave enough space internally (maybe use rasterImage instead of image) and use text to do the labelling. Look at the xpd argument to par if you want to add the lines and the grconvertX and grconvertY functions to help with the coordinates of the line segents.

Plotting fixed plot area

I have simple question, how can I plot fixed height barplots, i.e. stretching the plot area only change margins not the bararea, like the following:
> A <- 4
> plot (A)
> barplot(A, col = "green4")
When I strech are, bar area also get increases.
Edits: I want to keep the box size constant even the plot gets stretched.
by splitting the screen into multiple parts, you can achieve that partially:
split.screen(c(3,1))
A <- 4
barplot(A, col="green4")
Are you looking to just expand the y axis. Look at ylim?
What you might be looking for is to fix your aspect ratio. This can be achieved using asp:
barplot(A, col = "green4", asp = 1)
See also this post to R-help.
On a more philosophical note, when the height of the bar changes, there is no change in surface area. barplot only draws a sequence of bars, where the x axis is an ordinal (ordered categorical) variable which makes it impossible to calculate a surface area. The height of the bar is the only changing variable. I would recommend drawing these kinds of timeseries using a simple line plot.
So instead of:
a = runif(100)
b = 1:100
barplot(a)
use:
plot(b, a, type = "l")
or switch to my favorite plotting package, ggplot2:
require(ggplot2)
theme_set(theme_bw())
qplot(b, a, geom = "line")

Resources