[R] dotplot with library lattice
Jim Lemon
jim at bitwrit.com.au
Fri Oct 24 11:28:38 CEST 2014
On Thu, 23 Oct 2014 05:57:27 PM Matthias Weber wrote:
> Hello together,
>
> i have a short question. Maybe anyone can help me to create a
barplot in R
> with the package lattice.
>
> I have the following data as ouput values and the following code:
>
> Data (d):
>
> KOST Budget IST
> 1060 -2.18 0
> 1080 91037.71 91647.15
> 1100 955573.87 907938.98
> 1120 23326.8 0
> 1150 2521.57 0
> 1180 51302.03 48760.45
> 1200 2027.04 -1667.5
> 1210 2385.03 2386.06
> 1220 0 0
> 1250 528.87 0
> 1255 766.54 0
> 1260 12154.97 4861.41
> Gesamtbudget 1141622.25 1054236.55
>
> Code:
>
> ### read the data
>
> d$KOST <- ordered( d$KOST, levels = d$KOST)
>
> ### load lattice and grid
> require( lattice )
>
> ### setup the key
> k <- simpleKey( c( "Budget", "IST" ) )
> k$points$fill <- c("blue", "darkgreen")
> k$points$pch <- 21
> k$points$col <- "black"
> k$points$cex <- 1
>
> ### create the plot
> dotplot( KOST ~ Budget + IST , data = d, horiz = TRUE,
> par.settings = list(
> superpose.symbol = list(
> pch = 21,
> fill = c( "blue", "darkgreen"),
> cex = 3,
> col = "black"
> )
> ) , xlab = "Kostenstellenübersicht", key = k,
> panel = function(x, y, ...){
> panel.dotplot( x, y, ... )
> # grid.text(
> # unit( x, "native") , unit( y, "native") ,
> # label = x, gp = gpar( cex = .7 ) )
> } )
>
> The result look like the attached graph. But this is not exactly what I
> want. I want the "Budget" on the right side (100), and the "IST"-Value
in
> dependence of the "Budget" between 0 and 100. As a example. If
there is a
> budget over 100.000 and the "IST"-Value ist around 50.000, the blue
button
> should be on the right side and the green button right in the middle.
>
> Maybe anyone can help me.
>
> Thank you.
>
> Best regards.
>
> Mat
>
>
Hi Mat,
I must admit that I have probably misunderstood your request. I have
assumed that you want the Budget as the x axis and the KOST as the
y axis. You seemed to be asking for Budget to be always 100 and that
didn't make sense. For some reason the "scipen" option stopped
working for me after the first plot and so I couldn't get rid of the
scientific notation on the x axis. Also this was done in base graphics
rather than lattice. I also left the "Gesamtbudget" (Total budget) out as
it would have squeezed most of the dots even farther to the left.
However, it might help.
mwdat<-read.table(text=
"KOST Budget IST
1060 -2.18 0
1080 91037.71 91647.15
1100 955573.87 907938.98
1120 23326.8 0
1150 2521.57 0
1180 51302.03 48760.45
1200 2027.04 -1667.5
1210 2385.03 2386.06
1220 0 0
1250 528.87 0
1255 766.54 0
1260 12154.97 4861.41",
header=TRUE)
options(scipen=4)
par(las=1)
plot(mwdat$Budget,mwdat$KOST,main="IST against Budget",
xlab="Budget",ylab="KOST",
xlim=range(mwdat$Budget),ylim=range(mwdat$KOST),
type="n",yaxt="n")
abline(h=mwdat$KOST,lty=2,col="lightgray")
points(mwdat$Budget,mwdat$KOST,pch=19,col="blue",cex=3)
points(mwdat$IST,mwdat$KOST,pch=19,col="green",cex=3)
legend(400000,1250,c("Budget","IST"),pch=19,
col=c("blue","green"),bty="n")
options(scipen=0)
axis(2,at=mwdat$KOST)
par(las=0)
Jim
More information about the R-help
mailing list